Hello All!
I want try to get the distinct results of a model query so I use the following code:
$passengers = $this->modelsManager->createBuilder()
->columns('title, first_name, last_name')
->from('App\Models\Passengers')
->Where('first_name like :search: or last_name like :search:', ['search' => $search])
->limit(10)
->distinct(true)
->getQuery()
->execute();
But the result returned as "Phalcon\Mvc\Model\Row", so my "Passengers" modal getter not work.
foreach($passengers as $passenger)
{
echo $passenger->getTitle('formatted'); // Uncaught Error occured
}
Uncaught Error: Call to undefined method Phalcon\Mvc\Model\Row::getTitle()
Is there any solution exists to solve the issue?
My current workaround is as the follow:
$passengers_query = $this->modelsManager->createBuilder()
->columns('title, first_name, last_name=')
->from('App\Models\Passengers')
->Where('first_name like :search: or last_name like :search:', ['search' => $search])
->limit(10)
->distinct(true)
->getQuery()
->getSql();
// Base model
$passenger_model = new ReservesPassengers();
// Execute the query
$passengers = new Resultset(
null,
$passenger_model,
$passenger_model->getReadConnection()->query($passengers_query['sql'], $passengers_query['bind'], $passengers_query['bindTypes'])
);
foreach($passengers as $passenger)
{
echo $passenger->getTitle('formatted'); // Works!
}
Best regards.