Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



62.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();;

?



6.1k

Nikolay Mihaylov, thanks