We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

Out of memory when doing find()

Hello all.

I have this code:

$users = \Qello\Mvc\Model\Users\Users::find(array('limit' => array('number' => $this->usersPage, 'offset' => $page * $this->usersPage)));
foreach ($users as $val) {
    $user = $val->toArray();
    $user['profiles'] = $this->getProfiles($user['id']);
    .
    .
}

private function getProfiles($id) {
    $profiles = \Qello\Mvc\Model\Users\UserProfiles::find('user_id = '.$id);
        .
        .
    return profileList
}

I have in the users table 5k records. When i run my script PHP says "Out of memory".

When I change getProfiles function like this:

private function getProfiles($id) {
    $profiles = \Qello\Mvc\Model\Users\UserProfiles::query()->where('user_id = :user_id:')->bind(array('user_id' => $id))->execute();
        .
        .
    return profileList
}

all works fine.

Do I have something wrong in my first script or this is a bug.

Thanks in advance.



98.9k

This is likely happen because:

$profiles = \Qello\Mvc\Model\Users\UserProfiles::find('user_id = '.$id);

It's creating a PHQL statement for every $id, not sure how much memory you have allowed for your script execution, but it's not PHQL-friendly (http://docs.phalcon.io/en/latest/reference/models-cache.html#caching-of-phql-planning).

The second option reuses the same PHQL preparation and it's more secure in the case a dangerous $id is passed to the function.

You can also write as:

$profiles = \Qello\Mvc\Model\Users\UserProfiles::findByUserId($id);
$profiles = \Qello\Mvc\Model\Users\UserProfiles::find('user_id = ?0', 'bind' => array($id));


1.3k
Accepted
answer

You have right. But in my case i use ids provided from databaze. I think that sanitizing internal data is not so important.

My first script use more that 128Mb memory. Second use only 7Mb. Two scripts must do one thing, i think with the some mode. But the big memory usage is fact. I want undestand what is wrong. This is important for my project.

Thanks

This is likely happen because:

$profiles = \Qello\Mvc\Model\Users\UserProfiles::find('user_id = '.$id);

It's creating a PHQL statement for every $id, not sure how much memory you have allowed for your script execution, but it's not PHQL-friendly (http://docs.phalcon.io/en/latest/reference/models-cache.html#caching-of-phql-planning).

The second option reuses the same PHQL preparation and it's more secure in the case a dangerous $id is passed to the function.

You can also write as:

$profiles = \Qello\Mvc\Model\Users\UserProfiles::findByUserId($id);
$profiles = \Qello\Mvc\Model\Users\UserProfiles::find('user_id = ?0', 'bind' => array($id));


1.3k

Thanks for the replay. After several tests and thinking my point of view is the same. But for me sleel be odd this situation. I haven't similar experianse with any other ORM or database wraper. In any case your answer help me to make my project beter.

Thanks again.