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

Can I add a new property to resultset with array?

I have a resultset that return from paginator


 $Products = $paginator->getPaginate();

I am trying to assgin a new property for each of Product, but it is not working, I wondering is there a way to make it work?


    $imageList = array(
                'imagename1.jpg',
                'imagename2.jpg',
        );

    foreach($Products->items as $p){
        $p->imageList = $imageList;
    }

currently $p->imageList is not working.



34.6k
Accepted
answer

Resultsets are inmutable, you have to create an array for that:

$imageList = array(
        'imagename1.jpg',
        'imagename2.jpg',
);

$items = array();
foreach ($Products->items as $p) {
        $p->imageList = $imageList;
        $items[] = $p;
}


15.2k

thanks

Resultsets are inmutable, you have to create an array for that:

$imageList = array(
       'imagename1.jpg',
       'imagename2.jpg',
);

$items = array();
foreach ($Products->items as $p) {
       $p->imageList = $imageList;
      $items[] = $p;
}


15.2k

It's actually works now with

$imageList = array(
        'imagename1.jpg',
        'imagename2.jpg',
);

$items = array();
foreach ($Products->items as $p) {
        $p->imageList = (array) $imageList;
        $items[] = $p;
}

$Products->items = $items;

where I have to specify with (array) $imageList;

Resultsets are inmutable, you have to create an array for that:

$imageList = array(
       'imagename1.jpg',
       'imagename2.jpg',
);

$items = array();
foreach ($Products->items as $p) {
       $p->imageList = $imageList;
      $items[] = $p;
}