So I am messing with this shopping cart application called magento. Which uses the zend framework. Right now I am trying to better understand all the functionality by calling a lot of the class's and using a lot of the methods. I notice a lot of objects have nested objects within them and I am trying to access them so I can call the methods that are in them.

if your not understanding, here is a little small diagram

OBJECT1() -> OBJECT2()-> METHOD()

Object2 has a method I want but object2 is nested within object1

here is some simple code that retrieves data and then dumps all value into a var_dump()

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('entity_id', '134');
$products->load();
foreach($products as $_prod) {
var_dump($_prod->getData());
}

The getmodel method calls a class called catalog_product(). Its almost like doing this

$products = new catalog_product()

except they dont allow direct access when declaring the class so you have to do it through a getmodel() function

But to make this all short. the addattributetofilter sets up a query which will eventually be ran in mysql and the load() method runs the query and returns the result. the var dump results look something like this

array(10) { ["entity_id"]=>  string(3) "134" ["entity_type_id"]=>  string(2) "10" ["attribute_set_id"]=>  string(1) "9" ["type_id"]=>  string(6) "simple" ["sku"]=>  string(8) "ac-66332" ["category_ids"]=>  string(2) "25" ["created_at"]=>  string(19) "2008-07-14 07:21:11" ["updated_at"]=>  string(19) "2008-07-14 07:21:11" ["stock_item"]=>  object(Mage_CatalogInventory_Model_Stock_Item)#12 (10) { ["_eventPrefix:protected"]=>  string(13) "core_abstract" ["_eventObject:protected"]=>  string(6) "object" ["_resourceName:protected"]=>  string(27) "cataloginventory/stock_item" ["_resource:protected"]=>  NULL ["_resourceCollectionName:protected"]=>  string(38) "cataloginventory/stock_item_collection" ["_cacheTag:protected"]=>  bool(false) ["_data:protected"]=>  array(18) { ["item_id"]=>  string(3) "104" ["product_id"]=>  string(3) "134" ["stock_id"]=>  string(1) "1" ["qty"]=>  string(8) "100.0000" ["min_qty"]=>  string(6) "0.0000" ["use_config_min_qty"]=>  string(1) "1" ["is_qty_decimal"]=>  string(1) "0" ["backorders"]=>  string(1) "0" ["use_config_backorders"]=>  string(1) "1" ["min_sale_qty"]=>  string(6) "1.0000" ["use_config_min_sale_qty"]=>  string(1) "1" ["max_sale_qty"]=>  string(8) "100.0000" ["use_config_max_sale_qty"]=>  string(1) "1" ["is_in_stock"]=>  string(1) "1" ["low_stock_date"]=>  string(19) "0000-00-00 00:00:00" ["notify_stock_qty"]=>  NULL ["use_config_notify_stock_qty"]=>  string(1) "1" ["product"]=>  object(Mage_Catalog_Model_Product)#98 (21) { ["_attributeUpdates:protected"]=>  array(0) { } ["_typeInstance:protected"]=>  NULL ["_linkInstance:protected"]=>  NULL ["_priceModel:protected"]=>  object(Mage_Catalog_Model_Product_Price)#79 (4) { ["_data:protected"]=>  array(0) { } ["_origData:protected"]=>  NULL ["_idFieldName:protected"]=>  NULL ["_isDeleted:protected"]=>  bool(false) } ["_urlModel:protected"]=>  object(Mage_Catalog_Model_Product_Url)#57 (4) { ["_data:protected"]=>  array(0) { } ["_origData:protected"]=>  NULL ["_idFieldName:protected"]=>  NULL ["_isDeleted:protected"]=>  bool(false) } ["_cacheTag:protected"]=>  string(15) "catalog_product" ["_eventPrefix:protected"]=>  string(15) "catalog_product" ["_eventObject:protected"]=>  string(7) "product" ["_cachedLinkedProductsByType:protected"]=>  array(0) { } ["_linkedProductsForSave:protected"]=>  array(0) { } ["_errors:protected"]=>  array(0) { } ["_superAttributeCollection:protected"]=>  NULL ["_superLinkCollection:protected"]=>  NULL ["_defaultValues:protected"]=>  array(0) { } ["_resourceName:protected"]=>  string(15) "catalog/product" ["_resource:protected"]=>  NULL ["_resourceCollectionName:protected"]=>  string(26) "catalog/product_collection" ["_data:protected"]=>  array(10) { ["entity_id"]=>  string(3) "134" ["entity_type_id"]=>  string(2) "10" ["attribute_set_id"]=>  string(1) "9" ["type_id"]=>  string(6) "simple" ["sku"]=>  string(8) "ac-66332" ["category_ids"]=>  string(2) "25" ["created_at"]=>  string(19) "2008-07-14 07:21:11" ["updated_at"]=>  string(19) "2008-07-14 07:21:11" ["stock_item"]=>  object(Mage_CatalogInventory_Model_Stock_Item)#12 (10) { ["_eventPrefix:protected"]=>  string(13) "core_abstract" ["_eventObject:protected"]=>  string(6) "object" ["_resourceName:protected"]=>  string(27) "cataloginventory/stock_item" ["_resource:protected"]=>  NULL ["_resourceCollectionName:protected"]=>  string(38) "cataloginventory/stock_item_collection" ["_cacheTag:protected"]=>  bool(false) ["_data:protected"]=>  array(18) { ["item_id"]=>  string(3) "104" ["product_id"]=>  string(3) "134" ["stock_id"]=>  string(1) "1" ["qty"]=>  string(8) "100.0000" ["min_qty"]=>  string(6) "0.0000" ["use_config_min_qty"]=>  string(1) "1" ["is_qty_decimal"]=>  string(1) "0" ["backorders"]=>  string(1) "0" ["use_config_backorders"]=>  string(1) "1" ["min_sale_qty"]=>  string(6) "1.0000" ["use_config_min_sale_qty"]=>  string(1) "1" ["max_sale_qty"]=>  string(8) "100.0000" ["use_config_max_sale_qty"]=>  string(1) "1" ["is_in_stock"]=>  string(1) "1" ["low_stock_date"]=>  string(19) "0000-00-00 00:00:00" ["notify_stock_qty"]=>  NULL ["use_config_notify_stock_qty"]=>  string(1) "1" ["product"]=>  object(Mage_Catalog_Model_Product)#98 (21) { ["_attributeUpdates:protected"]=>  array(0) { } ["_typeInstance:protected"]=>  NULL ["_linkInstance:protected"]=>  NULL ["_priceModel:protected"]=>  object(Mage_Catalog_Model_Product_Price)#79 (4) { ["_data:protected"]=>  array(0) { } ["_origData:protected"]=>  NULL ["_idFieldName:protected"]=>  NULL ["_isDeleted:protected"]=>  bool(false) } ["_urlModel:protected"]=>  object(Mage_Catalog_Model_Product_Url)#57 (4) { ["_data:protected"]=>  array(0) { } ["_origData:protected"]=>  NULL ["_idFieldName:protected"]=>  NULL ["_isDeleted:protected"]=>  bool(false) } ["_cacheTag:protected"]=>  string(15) "catalog_product" ["_eventPrefix:protected"]=>  string(15) "catalog_product" ["_eventObject:protected"]=>  string(7) "product" ["_cachedLinkedProductsByType:protected"]=>  array(0) { } ["_linkedProductsForSave:protected"]=>  array(0) { } ["_errors:protected"]=>  array(0) { } ["_superAttributeCollection:protected"]=>  NULL ["_superLinkCollection:protected"]=>  NULL ["_defaultValues:protected"]=>  array(0) { } ["_resourceName:protected"]=>  string(15) "catalog/product" ["_resource:protected"]=>  NULL ["_resourceCollectionName:protected"]=>  string(26) "catalog/product_collection" ["_data:protected"]=>  *RECURSION* ["_origData:protected"]=>  array(8) { ["entity_id"]=>  string(3) "134" ["entity_type_id"]=>  string(2) "10" ["attribute_set_id"]=>  string(1) "9" ["type_id"]=>  string(6) "simple" ["sku"]=>  string(8) "ac-66332" ["category_ids"]=>  string(2) "25" ["created_at"]=>  string(19) "2008-07-14 07:21:11" ["updated_at"]=>  string(19) "2008-07-14 07:21:11" } ["_idFieldName:protected"]=>  string(9) "entity_id" ["_isDeleted:protected"]=>  bool(false) } } ["_origData:protected"]=>  array(17) { ["item_id"]=>  string(3) "104" ["product_id"]=>  string(3) "134" ["stock_id"]=>  string(1) "1" ["qty"]=>  string(8) "100.0000" ["min_qty"]=>  string(6) "0.0000" ["use_config_min_qty"]=>  string(1) "1" ["is_qty_decimal"]=>  string(1) "0" ["backorders"]=>  string(1) "0" ["use_config_backorders"]=>  string(1) "1" ["min_sale_qty"]=>  string(6) "1.0000" ["use_config_min_sale_qty"]=>  string(1) "1" ["max_sale_qty"]=>  string(8) "100.0000" ["use_config_max_sale_qty"]=>  string(1) "1" ["is_in_stock"]=>  string(1) "1" ["low_stock_date"]=>  string(19) "0000-00-00 00:00:00" ["notify_stock_qty"]=>  NULL ["use_config_notify_stock_qty"]=>  string(1) "1" } ["_idFieldName:protected"]=>  string(7) "item_id" ["_isDeleted:protected"]=>  bool(false) } ["is_salable"]=>  string(1) "1" } ["_origData:protected"]=>  array(8) { ["entity_id"]=>  string(3) "134" ["entity_type_id"]=>  string(2) "10" ["attribute_set_id"]=>  string(1) "9" ["type_id"]=>  string(6) "simple" ["sku"]=>  string(8) "ac-66332" ["category_ids"]=>  string(2) "25" ["created_at"]=>  string(19) "2008-07-14 07:21:11" ["updated_at"]=>  string(19) "2008-07-14 07:21:11" } ["_idFieldName:protected"]=>  string(9) "entity_id" ["_isDeleted:protected"]=>  bool(false) } } ["_origData:protected"]=>  array(17) { ["item_id"]=>  string(3) "104" ["product_id"]=>  string(3) "134" ["stock_id"]=>  string(1) "1" ["qty"]=>  string(8) "100.0000" ["min_qty"]=>  string(6) "0.0000" ["use_config_min_qty"]=>  string(1) "1" ["is_qty_decimal"]=>  string(1) "0" ["backorders"]=>  string(1) "0" ["use_config_backorders"]=>  string(1) "1" ["min_sale_qty"]=>  string(6) "1.0000" ["use_config_min_sale_qty"]=>  string(1) "1" ["max_sale_qty"]=>  string(8) "100.0000" ["use_config_max_sale_qty"]=>  string(1) "1" ["is_in_stock"]=>  string(1) "1" ["low_stock_date"]=>  string(19) "0000-00-00 00:00:00" ["notify_stock_qty"]=>  NULL ["use_config_notify_stock_qty"]=>  string(1) "1" } ["_idFieldName:protected"]=>  string(7) "item_id" ["_isDeleted:protected"]=>  bool(false) } ["is_salable"]=>  string(1) "1" } 

    if you look at the results you will find querys like this

    ["product"]=> object(Mage_Catalog_Model_Product)#98

    This object contains methods like

    getName()

    getPrice()

    and such. Which Im trying to access. this is code i run to try to retieve it

    $products = Mage::getModel('catalog/product')->getCollection();
    $products->addAttributeToFilter('entity_id', '134');
    $the_product = $products->load();
    echo $the_product->getName();  

    But I get this error

    Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection::getErrors() in C:\Windows\EasyPHP 2.0b1\www\magento\practice.php on line 48
    

    I know alot of you guys arent too formiluar with these methods or even the framework but question only has to do with trying to obtain objects that are nested within objects that have methods. how do I go about doing this ??? does anyone have any idea what i may be doing wrong. thanks alot

      2 years later

      Actually is $products->addFieldToFilter('entity_id', '134');

      addAttributeToFilter is wrobng method name, at least in the actual versions.

        Rolando, glad your willing to help out but I think if he hasn't got it figured out after 2 years he either gave up or tried something else. 😃

          Write a Reply...