How to merge two collections in magento?

4,202 views
Skip to first unread message

Sczeng

unread,
Dec 14, 2010, 6:25:20 AM12/14/10
to Magento Development List
Hi,

Is there any way to merge two collection in magento.

$collectionProduct1 = Mage::getResourceModel('reports/
product_collection'); //Line 1
/** Custom settings for $collectionProduct1 **/

$collectionProduct2= Mage::getResourceModel('reports/
product_collection'); //Line 2
/** Custom settings for $collectionProduct2**/

Adding them simply will result in normal addition of two php
variables. It will not
be considered as "Mage_Reports_Model_Mysql4_Product_Collection"
object.

$resultCollection = $collectionProduct1 +
$collectionProduct2 //Line 3

return $resultCollection; //Line 4


Any help, please?

Leonardo Situmorang

unread,
Dec 14, 2010, 6:36:37 AM12/14/10
to magent...@googlegroups.com
Try union:

$collectionProduct1 = Mage::getResourceModel('reports/product_collection');   #1
//... custom settings ...

$collectionProduct2 = Mage::getResourceModel('reports/product_collection');   #2
... custom settings ...

$collectionProduct1>getSelect()->union(array($collectionProduct2->getSelect()));


--
You received this message because you are subscribed to the Google Groups "Magento Development List" group.
To post to this group, send email to magent...@googlegroups.com.
To unsubscribe from this group, send email to magento-deve...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/magento-devel?hl=en.




--
Leonardo Situmorang
http://leonardo.situmorang.net

Lee Bolding

unread,
Dec 14, 2010, 6:38:19 AM12/14/10
to magent...@googlegroups.com
You should be able to do something like...

foreach ($collectionProduct2 as $item)
{
$collectionProduct1->addItem($item);
}

return $collectionProduct1;

Although, a handy merge function to wrap this would be pretty cool... as would a fromArray function in the collection abstract class, so that we could simply use...

$collection = $collection->fromArray(array_merge($collection1->toArray(), $collection2->toArray());

Samia

unread,
Dec 14, 2010, 8:12:47 AM12/14/10
to magent...@googlegroups.com
Thanks!
 
 
As, both of my collection are not of different size & column, that's why i might be getting some log errors.
I believe, both collections should have same number of  columns for union to work with.

And when I am trying merge two collection through fromArray it gives me the error as in: 
"Call to undefined method Mage_Catalog_Model_Resource_Eav_Mysql4_Product::fromArray()"
 
Any further help, please?
--
 
 
 

Lee Bolding

unread,
Dec 14, 2010, 8:28:42 AM12/14/10
to magent...@googlegroups.com

On 14 Dec 2010, at 13:12, Samia wrote:

> And when I am trying merge two collection through fromArray it gives me the error as in:
> "Call to undefined method Mage_Catalog_Model_Resource_Eav_Mysql4_Product::fromArray()"
>

Yes, $collection->fromArray doesn't currently exist, it was a suggestion for an improvement

You'll need to use my first example, using the $collection->additem() function

Another possible improvement would be an addItems function...

$collection1->addItems($collection2->toArray())

then, extend the Varien_Data_Collection, adding :

function addItems(array $items)
{
foreach ($items as $item)
{
$this->addItem($item);

Reply all
Reply to author
Forward
0 new messages