Magento Forum

Page 1 of 2
Product Collection -> Varien_Object
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Hi Guys, I have been searching to find where a product collection gets converted from a result set into instances of Varien_Object just before the the collection gets returned as an ‘object’ collection. I cant seem to find the method and I have been traversing through the class hierarchy for hours now. Does anybody know where this happens? I feel like I keep skipping right over it.

Thanks

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

Also, I seem to have a kink in my logic. I have noticed that the catalog/category model contains the method getProductCollection().

This is the containing code:

public function getProductCollection()
    
{
        $collection 
Mage::getResourceModel('catalog/product_collection');

        return 
$collection;
    
}

This method returns a viable object collection containing instances of Varien_Object. I guess I dont understand how this can be. The model ‘catalog/product_collection’ is a class. We arent telling it to return anything. I dont understand how we could assign a collection to the variable $collection from this class. Yes, the class contains access to the product collection, but somehow by returning this class, the class ‘becomes’ the collection. Could somebody explain how this is happening? Perhaps there is a magic function that I am unaware of.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Magento
Magento Team
 
Avatar
Total Posts:  20
Joined:  2007-07-05
Los Angeles, CA
 

Mage::getResourceModel(’catalog/product_collection’) instantiates a class that eventually extends Varien_Data_Collection which implements built-in PHP interface IteratorAggregate, which makes the object iterable the same as an array by implementing getIterator() method.

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

Great post, thanks for the clarification. Now that I understand that, I wonder if you could clarify something else for me. I have noticed that both the category collection and the product collection stem from this object. I have noticed that there is an _init() method that is expecting a parameter of an object type to be passed. This would explain how the collection object would know what type of object the elements are to be.

I am a little confused about where the difference in tables is declared. For instance, the products reside in the catalog_product_entity table and the categories reside in the catalog_category_entity. Is there a parameter or method that tells the class to find records in the appropriate tables?

I would appreciate your input.

Thank you.

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

Alright, ignore that last post, I figured it out. To continue the previous notion of Varien_Data_Collection becoming iterable, I have another kink in my logic. So with a little research I understand the IteratorAggregate interface and why it is useful. I am still lacking to make a connection. Is the Varien_Data_Collection class making the connection to the database, or is some other object making the db request, and then making the result set an instance of this class?

Sorry if I am not making any sense, I really want to understand this concept.

Thanks again.

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

nevermind, I figured it out.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ravikumar N B
Sr. Member
 
Avatar
Total Posts:  213
Joined:  2009-01-12
 

Hi

I tried catalog/product_collection model, but i got null values for all the attributes values.

This is my test coding

$prod Mage::getResourceModel('catalog/product_collection');
var_dump($prod->getItemById(1)->getSku());
var_dump($prod->getItemById(1)->getPrice());

I got both(sku and price) values are NULL.

Any other idea to fetch the attribute values from collection or any errors in my coding???
snake

Thanks in advanced.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ravikumar N B
Sr. Member
 
Avatar
Total Posts:  213
Joined:  2009-01-12
 

~~~~~!!!BUMP!!!~~~~~

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ravikumar N B
Sr. Member
 
Avatar
Total Posts:  213
Joined:  2009-01-12
 

~~~~~~~!!!BUMP!!!~~~~~~~

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ravikumar N B
Sr. Member
 
Avatar
Total Posts:  213
Joined:  2009-01-12
 

I think there are no “magento” programmers here..
They all are busy in Enterprise Edition!!!???

 
Magento Community Magento Community
Magento Community
Magento Community
 
demonkoryu
Guru
 
Avatar
Total Posts:  328
Joined:  2008-11-26
Dortmund, Germany
 

Maybe there is no ID 1…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ravikumar N B
Sr. Member
 
Avatar
Total Posts:  213
Joined:  2009-01-12
 

$prod->getItemById(1)->getSku()
Product ID: 1

I tried with different IDs also, but response is same!!!!!!!!

Note:
Created a new custom module in app/code/local pool, and i didnt add any product_collection related values in config.xml

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ravikumar N B
Sr. Member
 
Avatar
Total Posts:  213
Joined:  2009-01-12
 

Any idea
:(
:(
:(

 
Magento Community Magento Community
Magento Community
Magento Community
 
piotrn
Member
 
Total Posts:  33
Joined:  2008-04-27
 

I have not tried it just now, but the standard way to get populated collection would look like that:

$productsCollection Mage::getModel("catalog/product")->getResourceCollection()->load();

If you have more problems with it, post it, I’ll enable email notification.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ravikumar N B
Sr. Member
 
Avatar
Total Posts:  213
Joined:  2009-01-12
 

Hi Piotrn

I tried your coding.

But i got the same result(NULL values)

smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
darryla
Member
 
Total Posts:  52
Joined:  2008-07-08
 

Try:

$product->load($product->getId());

then run commands against, $product.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2