Magento Forum

   
Multiple Collection Requests
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

When a product collection is requested, an instance of an object that eventually implements IteratorAggregate is returned. This object contains the method addAttributeToSelect(), which modifies the EAV object’s $_select variable.

It appears that when the collection object is instantiated, all products in the database are retrieved. Then, when the addAttributeToSelect() method is called, the collection retrieves the entire set again, but with additional attributes in the result set. Also, when a filter method is called, it appears that the entire collection is retrieved again.

So my question is, am I being deceived, or is the entire product result set being retrieved multiple times before the final set is used?

If this is the case, wouldnt it make sense to build the entire Zend_Db_Select object and apply all necessary filters before requesting the result set?

Please, correct me if I am wrong or overlooking something.

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

I think here’s an example of Observer Effect (http://en.wikipedia.org/wiki/Observer_effect) in action smile .

The collection will not be loaded until it is requested by $this->load() method.

As you have rightfully observed, collections implement IteratorAggregate interface which invokes getIterator() method on any action that would be performed on an iterator (foreach, count, etc)

It appears that when the collection object is instantiated, all products in the database are retrieved.

I’m guessing that the appearance is caused by invoking one of the actions that would trigger getIterator() during your debugging.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Thanks for your reply,

I have noticed that inside the getIterator() method is the $this->load(); statement, which loads the recordset. So, if the addAttributeToSelect() method is invoked, it would be invoked after the initial collection object has been created, and thus, after the recordset has been retrieved.

Am I still incorrect? Wouldnt it be possible to exclude the $this->load() statement, then after the collection object has been instantiated, and all attributes and filters have been placed, then manually call the load method to load the set?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

So, if the addAttributeToSelect() method is invoked, it would be invoked after the initial collection object has been created, and thus, after the recordset has been retrieved.

Incorrect, initial collection object creation does not invoke getIterator().

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top