简体   繁体   中英

Difference between Model and Observer

I am having trouble understanding the difference between an Observer and a Model. Are they really the same thing?

For instance, I have a Model that exports orders. I have an observer that listens for events (such as order placed), and then there is code in the controller to call the export order model.

Now, I have a controller that I want to perform the same function (of exporting orders based on the passed request).

Can I simply create an instance of the Observer (which already has a method to call the Export model), or do I need to create the code again to create the Model?

Model consists of the actual schema. [Data and Fields].

From your context

I have an observer that listens for events (such as order placed)

Anything that listens for events , etc is what a role of a " Controller ". So, A Model is totally different from that of an Observer.

In terms of definitions :-

In terms of usage in Magento

  • Models

    • Models extends ( another model classes ) or ( abstract class Mage_Core_Model_Abstract ) so it has Base class and method shared,extended,implemented from that base class.

    • Usually Models in Magento are associated with database tables and it implements ORM through Resources and Collection

    • XML Declaration :

    --

     <global> .............. <models> <modulename> <class>Package_Modulename_Model</class> <resourceModel>Modulename_resource</resourceModel> </modulename> <modulename_resource> <class>Package_Modulename_Model_Mysql4</class> <deprecatedNode>package_modulename_mysql4</deprecatedNode> <entities> <modulename> <table>db_table</table> </modulename> </entities> </modulename_resource> </models> ........ </global> 
  • Observers

    • Observer are normal classes that implement some method to watch for defined events that happen in during the execution of the script and it execute instructions defined in this methods ( functions ).
    • Observers can be stand alone classes or extends other classes to share some methods from it or any other possible purpose.

    • XML Declaration : Observer doesn't need to declared to be used in magento, you just need to specify the class name and method to be fired based on the dispatched event.

    --

     <events> <sales_quote_save_after> <observers> <observer_unique_name> <type>model</type> <class>Package_Modulename_Model_Observer</class> <method>quoteSaveAfter</method> </observer_unique_name> </observers> </sales_quote_save_after> </events> 

The above information is very short but it demonstrate the basic difference between the observer and the model.

you need to read and practice using this in magento with different use cases to be able to differentiate between both ( check the wiki links and google some tutorials )


As per the comment that you want to access the observer from the controller,You can do this in different ways

  • You can write the observer to Extend ( Mage_Core_Model_Abstract ) { You need to define the module model section in config.xml of the module }, Then you will be able to access the observer as Normal model Mage::getModel('modulename/modelname');

  • Write the observer as normal php class in the module ( ex: Package_Module_Model_Observer ) and then you can always use " new " operator to instantiate it $class = new Package_Module_Model_Observer()

A model notifies its associated views and controllers when there has been a change in its state. Model is business logic. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). observer is something that gets these notifications and update / react on it.

An observer is a Controller. It listens for a specified event and when that even is fired, the observer tells the Model what to do. Model handles data and the Controller tells what to do with the data.

In your case, I don't think you'd be creating an instance of the observer but rather create a controller and use the existing export model with it.

Please go through This Link . It has a detailed description regarding Models,Helpers and controllers in Magento.

Hope this helps !!!

The Model View Controller in Magento is implemented a bit arbitrarily. In Magento an observer is just a model that has a public method with a specific parameter that holds the data passed from the event, so yes they are the same thing. You can just create an instance and call the same method, but you will have to pass the same data that is passed to the event.

For example: in the event sales_order_place_after the order object is passed. You could do something like this:

$observer = Mage::getModel(‘yourmodule/observer’);

$observerData->setOrder($order);

$observerData = new Varien_Object();

$observer->exportOrder($observerData);

Here you will find some more details about the issue. This link also offers you a video of a presentation of Andreas von Studnitz and Dr. Nikolai Krambrock about code quality on the Meet Magento DE 2014: http://www.code4business.de/code-qualitaet-magento/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM