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

How change values of model resultset's items before render?

Hey guys!

How i can modify values in model result, before rendering?

it not work:

$items = Items::find();
foreach($items as $item) {
    $item->title = '[modified]' . $item->title;
    $item->summ = $item->summ * 2; 
}
edited Sep '17

The for loop creates a temporary variable, try it like this:

foreach($items as &$item) // note the ampersand before the $item var

Depending on your code, you could also create an afterFetch method on your model file, it will be called for each record that you fetch from db

class Items extend \Phalcon\Mvc\Model
{
    function afterFetch()
    {
        $this->title = '[modified] '. $this->title;
    }
}

But this code will run on EVERY Items model that you fetch

I think you should better to do the modification in the column selection on the query, you'll got better performance no needs to parse all results.
For your example :

$items = Items::find([
  'columns' => 'concat(\'[modifified]\', title) as title, (summ * 2) as summ '
]);