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

Modify find result

Hello,

I would like to add a new key to my find result, so i do :

$responses = Response::find();
foreach ($responses as $key => $value) {
    $dependance = Dependance::find([
        'id_dependance = :id_dependance:',
        'bind' => [
            'id_dependance' => $responses[$key]->id
            ]
    ]);

    $responses[$key]->Dependance = $dependance;

But if i do this in volt :

{% for response in responses %}
    {{ response.Dependance[0]->id }}
    <?php property_exists($response, 'Dependance') ? print 'true': print 'false'; ?>
{% endfor %

The first doesn't show anything, and the second returns false all time. I've check that the Dependance::find retunr s a result, and it does.

So how do i modify the result of a find to add a new key ??



11.6k

why not using relations, it sound more appropriate in your case... ? or you can use find()->toArray then add property using bracket notation



4.5k

Well i can't use relations, since the id_dependance points to different tables depending on the Type column (table dependance has : id, id_dependance, type, id_depend_of). And i've tried toArray() but then it doesn't return related models to responses in the array, only the response which is annoying;

Just use query builder.