We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Is there a better alternative?

Hello everyone. I need to get single record from database as array. When I use the this code, error comes out "Fatal error: Cannot use object of type App\Entity\City as array in"

$city = \App\Entity\City::findFirst([
    'conditions' => "id = :id:",
    'bind' => [
        'id' => $id,
    ],
    'hydration' => \Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS
]);

I found that findFirst method does not support hydration

Now I use the this code

$result = \App\Entity\City::find([
    'conditions' => "id = :id:",
    'bind' => [
        'id' => $id,
    ],
    'limit' => 1,
    'hydration' => \Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS
]);

$result->rewind();
$city = $result->current();

Is there a better alternative?



93.7k
Accepted
answer
edited Mar '16

Perhaps:

$city = \App\Entity\City::findFirst(...);
$city->toArray();

// Even shorter solution (searches by PK)
$city = \App\Entity\City::findFirst(123)->toArray();;

?



12.4k

Nikolay Mihaylov, thanks