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

merge 2 object

Hello everyone

I have a function as below:

    columns("Items.id,Items.item_name,Items.item_image,
    Items.item_promo,Items.item_price,Items.item_price_tl,Items.item_currency,Items.user_type,Items.user_id,Ite ms.referer,Items.item_stock,Items.item_category,Partners.url,Partners.discount_code")
            ->from('Items')
            ->join("Partners","Partners.id = Items.user_id")
            ->where("Items.id IN (".$idsStr.")")
            ->andWhere("Items.item_stock = 1")
            ->orderBy("FIELD(Items.id, {$idsStr})")
            ->getQuery()
            ->execute();

            return $select;

I run that command two times and want to merge them into two different returns. How can I merge two object(Phalcon\Mvc\Model\Resultset\Simple) ?



51.1k

You don't combine 2 objects, but you can combine 2 arrays. In theory, if the object does not have methods, you could do something like

$obj = (object) array_merge((array) $objA, (array) $objB);

But in this case, the object has methods. So what you can do would be

$objA = $resultA->toArray();
$objB = $resultB->toArray();

$output = array_merge($objA, $objB);

Maybe someone else has other ideas.

$output = array_merge($objA, $objB);

You may be able to convert them back into a Resultset through one of the cloneResultMap, cloneResultMapHydrate or cloneResult methods : https://docs.phalcon.io/en/3.0.0/api/Phalcon_Mvc_Model.html