Getting things in Magento by getModel and getData methods

 

One of my previous articles was about getting a product information using getData method. This article is a step forward in that direction. I’m gonna show you how to retrieve almost anything in Magetno using getModel and getData methods (function if you prefer).

Before I start with the title related content let me say a word or two of my development tool. For the last few weeks I’m using only one tool while developing on PHP platform, NetBeans 6.5. My favorite text editor still remains Notepad++. Although the the official release is NetBeans 6.5 beta, I’m using the night build versions. In my modest opinion this is the best free IDE solution currently out there. I simply love the code completion in PHP and in HTML plus the coolest thing ever, jQuery code completion support. Since I’m a big fan of jQuery, NetBeans is my number one choice. Some of you probably don’t see HTML code completion as that big of a deal. Well, I do. It saves you a lot of time finishing all those quotes next to attributes and auto closing all those markup.

There is quite a number of threads and discussions on Magento form about getModel and getData methods. Here I will show you a few ways of using both. One thing to have in mind. I’m working on locally installed Magento with default sample data package. For some reason I like to use the app/design/frontend/default/mycustom/template/catalog/product/view.phtml file.

Where mycustom in the folder name is the name of my theme folder. Let’s get started.

If you open the view.phtml file and write down the $cModel = Mage::getModel(’catalog/product’); you would basically say create an instance of Mage_Catalog_Model_Product class. So where does the catalog/product comes from? What are the other available parameters one can put into the getModel function? What are the methods of I can use on this new instance object? All of these are logical questions. Questions so poorly covered by current Magento documentation.

As I sad it before, I said it again; Magetno is great software, it’s biggest flaw is the current lack of good documentation. Let us try to answer the questions mentioned above.

Where does the catalog/product parameter comes from? If you browse to /app/code/core/Mage/ folder you would see a list of logically named subfolders like Catalog, Checkout, Contacts, Core, Dataflow and so on. If you browse deeper into the Catalog folder, you will see it’s structure contains Model directory among all others. Browse into the Model directory and there you will see a bunch of files, among which is Product.php. This Product.php is whats called when you write down getModel(’catalog/product’). In short getModel(’catalog/product’) says go into the /app/code/core/Mage/ folder, open the folder named product, and create an instance of Product.php file located inside the Model subfolder (to be more precise, create the instance of the class cotained inside the Product.php file).

If we now echo the get_class($cModel); we would get the Mage_Catalog_Model_Product text displayed in our browser. This means that our $cModel variable is actually an instance of Mage_Catalog_Model_Product class. This matches the class name contained inside the app/code/core/Mage/Catalog/Model/Product.php file. Inside this Product.php file we can clearly see that Mage_Catalog_Model_Product class extends the Mage_Catalog_Model_Abstract class. What this means is that our $cModel can use all of the methods (functions) declared inside the Mage_Catalog_Model_Product class plus those declared inside the Mage_Catalog_Model_Abstract class.

To see the list of the methods available you can use IDE like NetBeans. NetBeans lists you all the methods of a class in the Navigator window, like the one provided in the screenshot. Or you can use the foreach loop to iterate and echo out the available method names like

echo ‘<ul>’;
foreach (get_class_methods(get_class($cModel)) as $cMethod) {
echo ‘<li>’ . $cMethod . ‘</li>’;
}
echo ‘</ul>’;

You can look at the screenshot for the result. You can see some pretty cool and useful methods like getName, getPrice, getTypeId, getStatus which we can execute on our $cModel variable like

echo ‘Product name: ‘ . $cModel->getName();
echo ‘Product price: ‘ . $cModel->getPrice();

However, the above code wont give you anything. You will not see any price displayed in the browser. Why, you might and should ask? Well, for what product are you requesting a price? Magento does not know that at this point. Some of you might ask, how is it that it does not know? We have a product view page open after all. Well, we do, but I’m showing you a code that you can use no matter what page you have open.

We can tell it to him in a single line of code using load() method. This method is also displayed on the list of available methods for this variable. So the above code fore retrieving the product name and price would work if we were to write it like

$cModel->load(53);
/*
Number 53 is the id number in default sample data, it’s assigned to Couch product you can see in this screenshots
*/

echo ‘<p>Product name: ‘ . $cModel->getName() . ‘</p>’;
echo ‘<p>Product price: ‘ . $cModel->getPrice() . ‘</p>’;

View.phtml file already has the instance of Mage_Catalog_Model_Product class trough $_product = $this->getProduct() variable. Here you can see the Magento creaters are using the getProduct() method on the $this variable. Variable $this contains huge amount of data in it, among which is current instance of  Mage_Catalog_Model_Product class stored in some array structure. If you were to retrieve the list of available methods of $this variable, like we did on the $cModel variable, you were to see the getProduct() method among them like

echo ‘<ul>’;
foreach (get_class_methods(get_class($this)) as $cThis) {
echo ‘<li>’ . $cThis . ‘</li>’;
}
echo ‘</ul>’;

If you understood all of the stuff I showed you so far then you understand the power of getModel method. You create the instance of class and store it into some variable (like $cModel), load the data for that type of object by using load function, then ust the list of available functions on that variable (object).

Title of this post mentions getData method also. As you can see, getData is just one of multiple available methods for your new variable. So why mentioning it in the same context as getMethod?

Before I answer that question let me just explain how getData can be executed with or without any parameters passed to it. If you execute it without any parameters you get the array variable as a result. You can’t directly echo the array. You would have to map it to some field, like echo $arayVar['someField']. So I ask you, what are all the available fields? To find out you can do something like

echo ‘<pre>’;
print_r($cModel->getData());
echo ‘</pre>’;

Results are so tasteful. There is a bunch of data one can use in it’s custom design solutions. Let’s say you want to retrieve the SKU value. You can use some other method for that like getSku() method that needs to be executed on object of Product type or you can

echo $cModel->getData(’sku’);

or

$cModelData = $cModel->getData();
echo $cModelData->sku;

Cool, right. If you look at the entire array of data thrown at us when we executed print_r($cModel->getData()); we can clearly see there are some nested arrays, and some array fields store entire objects, like stock_item field. It stores object of Mage_CatalogInventory_Model_Stock_Item type. When you run into stuff like this, you can do the following

$cStockItem = $cModel->getData(’stock_item’);
or
$cStockItem = $cModelData->stock_item;

Now your $cStockItem variable is a object of type Mage_CatalogInventory_Model_Stock_Item and you can go ahead and use the same principle used until this point to find it’s methods and the data id holds.

Hope this was useful.

發佈了66 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章