Magento, Where is getOrderedQty() Function? - magento

Hello I have trying to learn Magento, So would be appreciated if anyone helps.
I saw people initiate class
Mage::getResouceModel('report/product_sold_collection');
Then they iterate over the collection using foreach loop and use
getOrderedQty() method.
Like in this thread.
total sales of each product in magento
Where is this method defined? In which class ?

That method doesn't exist. All Magento Objects (that is, objects which inherit from Varien_Object) allow you to get and set data properties.
$object->setData('the_thing',$value);
echo $object->getData('the_thing')
On top of this, there's also special setter and getter methods implemented with PHP's "magic methods". That is, you can get/set a data property by camel casing it's name, and calling it like this
$object->setMyThing($value);
echo $object->getMyThing();
I searched the Magento codebase, and there's no definition for a "getOrderedQty" method. That means it's one of the above mentioned magic methods.

Related

Having trouble navigating Magento documentation

I am brand new to Magento and the documentation, primarily the phpDocs, are difficult to navigate. For example,
$attributeSet = Mage::getModel('eav/entity_attribute_set')->load($id);
In the php doc for Class Mage_Eav_Model_Entity_Attribute_Set there is no mention of the method getAttributeSetName() either in inherited methods or otherwise and yet this works.
$attributeSet = Mage::getModel('eav/entity_attribute_set')->load($id);
echo $attributeSet->getAttributeSetName();
So I suppose I have several questions.
Can someone explain to me why the documentation is this way?
Where I can find the mysterious getAttributeSetName() method in the phpDocs?
My theory is that there is some inheritance or a design pattern implementation going on that I'm not understanding, maybe someone can shed some light on this for me.
If you really want to fry your brain, take a look at the source code for Mage_Eav_Model_Entity_Attribute_Set and follow the inheritance chain all the way back. You won't find a getAttributeSetName method defined anywhere.
All Magento objects that inherit from Varien_Object can have arbitrary data members set on them. Try this.
$attributeSet = Mage::getModel('eav/entity_attribute_set')->load($id);
$attributeSet->setFooBazBar('Value');
var_dump($attributeSet->getFooBazBar());
var_dump($attributeSet->getData('foo_baz_bar'));
var_dump($attributeSet->setData('foo_baz_bar','New Value'));
var_dump($attributeSet->getFooBazBar());
You can also get all the data members by using
var_dump($attributeSet->getData());
but be careful dumping these, because if there's a data object that has a circular reference and you're not using something like xDebug, then PHP will have a fit trying to display the object.
Magento stores data properties in a special _data array property. You can get/set values in this array with getData and setData. Magento also has implemented magic getting and setter methods, so when you say something like
$object->getFooBazBar();
The method getFooBazBar is transformed into the data property foo_baz_bar. and then getData is called using this property. It's a little tricky to get your head around, but once you get it you'll start to see how much time you can save using this pattern.
One side effect of this is, of course, it's impossible to infer what data properties any object might have by looking at it's class file, so there's no phpDocs for these methods.

Magento difference between collection and resource collection

In Mage_Core_Model_Abstract there is the getCollection method that is widely used.
But, I had never looked at the getCollection method until now. I can see that all it does is call $this->getResourceCollection()
What is the point in having getCollection and why don't everyone just use getResourceCollection instead?
As for my point of view, it just gives me more clean code for model collection retrieval, without unnecessary typing of word Resource all the time. Also if you look into Order model or Quote model, you will see that it also using simplified method name for collection retrieval of items, addresses, payments, etc.
If it would be named getItemResourceCollection(), getAddressResourceCollection() instead of getAddressCollection() or getItemsCollection(), amount of characters you type during development is increasing. There were no explanation about why getCollection() should be used in favor getResourceCollection() while I was working in core team, but it was quite logical for me to use shorter name of method.
At least non of these methods are marked as deprecated, so you can use them both.

Magento getSingleton confusion

I'm a little confused about calls I see to Mage::getSingleton, and I'm hoping someone can help me understand a little better.
I have seen a piece of core code that does this:
Mage::getSingleton('customer/session')->isLoggedIn()
I don't know PHP, but I think I can make a safe assumption from the getSingleton method name that there will be only one instance of the class specified (the class being specified as a grouped class name, and resolving to app/code/core/Mage/Customer/Model/Session.php - containing class Mage_Customer_Model_Session.
Question 1 -
How did the getSingleton method know to look in the Model folder for the class?
Question 2 -
So there is one instance of the class for the whole ... I want to say JVM as I am from a Java background, but I'll say PHP engine in the hope that that is vaguely the correct terminology; the Mage_Customer_Model_Session is not passed in a customer id or any such identifier, yet we call the method isLoggedIn()! Give there is not a Mage_Customer_Model_Session instance per customer, how can we ask a singleton if a customer is logged in when we do not tell it what customer we are talking about?
Question 3 -
I've seen calls to Mage::getSingleton('core/session') and to Mage::getSingleton('customer/session') - what is the difference?
Thank you for any help.
First, before we get to Magento, it's important to understand that PHP has a radically different process model than Java.  A PHP singleton (regardless of Magento's involvement) is a single instance of a class per HTTP Request.  A PHP program isn't persistent in memory the same way a Java program is, so adjust your expectations of a "singleton" accordingly.   
Next, it's important to understand that Magento is a framework built on top of PHP, using PHP, and in many cases the original Magento developers wanted to push things towards a more Java like architecture.  So, you're going to see things that look familiar, are familiar, but likely differ in some major way from what you're used to because they still need to hew to PHP's version of the universe. 
Magento uses a factory pattern to instantiate Helpers, Blocks, and "Model" classes.  The string
core/session
is a class alias.  This alias is used to lookup a class name in Magento's configuration. In short, this string is converted into path expressions that search Magento's configuration files to derive a classname, based on the context (helper, block, model) it was called in. For a longer version, see my Magento's Class Instantiation Autoload article.
The concept of a "Model" is a little fuzzy in Magento.  In some cases models are used as domain, or service models.  In other cases they're used as a more traditional middleware database persistence models.  After working with the system for a few years, I think the safest way to think about Models is they're Magento's attempt to do away with direct class instantiation.
There's two ways to instantiate a model class. 
Mage::getModel('groupname/classname');
Mage::getSingleton('groupname/classname');
The first form will get you a new class instance.  The second form will get you a singleton class instance.  This particular Magento abstraction allows you to create a singleton out of any Magento model class, but only if you stick to Magento's instantiation methods.  That is, if you call 
Mage::getSingleton('groupname/classname');
then subsequent calls to 
Mage::getSingleton('groupname/classname');
will return that singleton instance.  (This is implemented with a registry pattern). However, there's nothing stopping you from directly instantiating a new instance of the class with either
$o = Mage::getModel('groupname/classname');
$o = new Mage_Groupname_Model_Classname();
Which brings us to sessions.  PHP's request model, like HTTP, was originally designed to be stateless.  Each request comes into the system with, and only with, information from the user.  As the language (and the web) moved towards being an application platform, a system that allowed information to be persisted was introduced to replace the homegrown systems that were cropping up.  This system was called sessions.  PHP sessions work by exposing a super global $_SESSION array to the end-user-programmer that allow information to be stored on a per web-user basis.  Sessions are implemented by setting a unique ID as a cookie on the user end, and then using that cookie as a lookup key (also standard practice for web applications)
In turn, the Magento system builds an abstraction on top of PHP's session abstraction.  In Magento, you can create a "session model" that inherits from a base session class, set data members on it, and save/load those data members just as you would with a database persistence model.  The difference is information is stored in the session instead of the database store. When you see
core/session
customer/session
these are two different session models, with each one storing different data. One belongs to the Mage_Core module, the other belongs to the Mage_Customer model.  This systems allows modules to safely set and manipulate their own session data, without accidentally stepping on another module's toes, and provide logical class methods for manipulating that data.
Hopefully that answers the questions you asked, as well as the ones you didn't.
Magento's getSingleton is almost the same as getModel. The difference is getModel always returns a new instance of a class, and getSingleton creates a new instance of a class only once and then always returns this instance. See the Mage::getSingleton and Mage::getModel methods.
Magento knows about looking to the Model folder because of configs in the config.xml file (f.e. Mage/Customer/etc/config.xml). See the Magento wiki for developers to know more about config files.
You do not specify customer directly. It's done automatically by Magento in parent classes of Mage_Customer_Model_Session (see Mage_Core_Model_Session_Abstract_Varien::start() method)
Magento has not one session class to discriminate session data. For example, customer ID is stored in Mage_Customer_Model_Session and error flash message 'Product is not available' will be stored in the Mage_Catalog_Model_Session class.

Where in Mage Core is a product's status set?

I'm trying to extend the core to change how magento sets the status on products. Essentially, when an admin user tries to change the status of a product to disabled, I want it to check to see if the product is in stock in their EPOS system, and if it is, throw an error.
To do this, I was going to extend the model where the product status is set and rewrite that function. The problem is, I can't find this anywhere. There is nothing in magento_core_model_product. I found a function in mage_catalog_model_product_status called updateProductStatus, but this doesn't seem right either.
Does anybody know where I need to be looking to find this function?
After a bit of research, I found that Magento generates all the getters and setters pragmatically, through extensive use of the __call() function, which is called when a function that isn't defined is called.
To modify functionality of a getter or setter, simply define the function you want to modify in your rewrite of the class, and this will be called before the __call(), essentially rewriting the default functionality, in a roundabout way.

Magento Dispatching and Catching Events

I am dealing with Magento for a while and i find it very interesting and probably my future choice of work tool. Though i have some troubles understanding some of the stuff going on. If i call www.store.com/catalog/product/view/id/2 then the product controller executes from the catalog core module, in it the product is being fetched via _initProduct() method first in which this event is being dispatched:
Mage::dispatchEvent('catalog_controller_product_init_before', array('controller_action'=>$this));.
Which class/method is being called? As i understood that should be a method from observer class which is under Model folder and it should be defined in the etc/config.xml file.
Some of the events defined in the config.xml are executed automatically... (why?) where is defined the one used in the viewAction() from the ProductController.php in Catalog module? How i can send and use array data to the observer's methods, cause i saw some of them contains this method: Mage::app()->reinitStores() which re-inits store, group and website collections and it's not something simple. I find this very powerful and i really want to know the posibilities with using Observers and Events.
Event observers can be defined in the config.xml for any module that is active in the system, they don't necessarily have to be defined in the same module.
You can send data to the event observers by adding information to the event object, which is done in an array defined as the second argument to dispatchEvent. Just add more elements to the array and the event observer method can extract it from $observer->getEvent(). You are also free to define your own events by calling the same dispatchEvent method.
One of the handy things about most Magento models is that they are inherited from the Mage_Core_Model_Abstract class which includes event for _load_after, _save_before, _save_after, _delete_before and _delete_after. For example, the product model has catalog_product_load_after, catalog_product_save_before, etc.
Hope that gives you some more information about the possibilities of using events.

Resources