Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Collection loading misunderstanding
 
jcalais
Jr. Member
 
Total Posts:  10
Joined:  2008-01-23
 

Hi,

I created a module extending some Magento class…

I instantiated a collection using the getModel()->getCollection syntax.

I added filters and field selection

What happens is that data gets loaded in the collection without having to call the load function. I do not understand why. I am afraid that data gets updated in the collection every time I add a filter.

Could someone explain me what is happening ?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
jcalais
Jr. Member
 
Total Posts:  10
Joined:  2008-01-23
 

up… please someone to help me understand ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
nicolas46
Sr. Member
 
Total Posts:  210
Joined:  2008-04-09
Toulouse
 

Don’t worry the collection is not update every time you add a filter. Look the file : app/code/code/Mage/Eav/Model/Entity/Collection/Abstract.php

What i think is that the load() function is automaticaly call when you try to reach the data (i’m not an expert but I thinks it’s possible)

Someone else has a real explanation ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
jcalais
Jr. Member
 
Total Posts:  10
Joined:  2008-01-23
 

True that what I think.

But I’d like to understand when data gets loaded into the collection. Is it done using an event or something ?

I think it is a point which remains undocumented and that is important to get a deeper understanding on how Magento works.

We bought magento book and the load function is called as soon as a getCollection is called. But in many examples found in wiki, it is not the case.

I would be great that magento team gives us some explanations on this point.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jcalais
Jr. Member
 
Total Posts:  10
Joined:  2008-01-23
 

up… Magento team on vacation ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Anton Makarenko
Magento Team
 
Avatar
Total Posts:  184
Joined:  2008-05-13
Los Angeles, CA
 

Collections implement IteratorAggregate interface and designed in a such way, that they will be loaded when treated as array. Foreach, count() in particular.
See Varien_Data_Collection_Db and Varien_Data_Collection

Of cause, you can call load() manually.

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
Joined:  2007-12-13
Atlanta, Georgia
 

How do you display the data for each item in a collection. For example:

$addresses Mage::getModel('customer/address')->getCollection();
foreach(
$addresses as $a){

}

How would I print the data about each address in the foreach loop?

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