Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there any decent way to Decorate models returned from a Magento `[model]_load_after`event?
    primarykey
    data
    text
    <p>I'm trying to overwrite some methods in models, and I'm on a mission to avoid overwrites and rewrites of models for maximum compatibility with other modules.</p> <p>I figured the best way would be to simply decorate models after they are loaded from Magento, however as far as I can tell because of the way the observer pattern in Magento is written it's impossible to accomplish this. ( As Magento always returns the reference to <code>$this</code> ), and the lack of interfaces might also cause trouble later down the road? See this partial of Mage/Core/Model/Abstract.php</p> <pre><code>/** * Processing object after load data * * @return Mage_Core_Model_Abstract */ protected function _afterLoad() { Mage::dispatchEvent('model_load_after', array('object'=&gt;$this)); Mage::dispatchEvent($this-&gt;_eventPrefix.'_load_after', $this-&gt;_getEventData()); return $this; } </code></pre> <p>My question boils down to the title, is there a decent way of accomplishing this?, or am I simply stuck with rewrites :(?</p> <p>The path I would like to take is;</p> <ul> <li>On event <code>[model]_load_after</code></li> <li>return <code>new Decorator($event-&gt;getObject())</code></li> </ul> <p>Where the decorator class in my case would be something like;</p> <pre><code>public function __construct(Mage_Sales_Model_Order_Invoice $model) { parent::__construct($model); // sets $this-&gt;model on parent class, see below } // overwrite the getIncrementId method public function getIncrementId() { return '12345'; } // partial of parent class public function __call($method, array $args) { return call_user_func_array(array($this-&gt;model, $method), $args); } </code></pre> <p>And just some pseudo-code for extra clarification;</p> <pre><code>$model = Mage::getModel('sales/order_invoice')-&gt;load(1); echo get_class($model); Namespace_Decorator **INSTEAD OF** Mage_Sales_Model_... echo $model-&gt;getIncrementId(); '12345' **INSTEAD OF** '1000001' ( or whatever the format might be ) </code></pre> <p>Thanks for your time reading / commenting, I really hope there actually <em>is</em> a way to accomplish this in a clean fashion without making use of code overrides or rewrites of models.</p> <p><strong>Edit: extra clarification</strong></p> <p>Basically what I would like is to return an instance of the Decorator in a few cases, the sales_invoice being one of them and customer the other. So when any load() call is made on these models, it will always return the instance of the Decorator instead of the Model. Only method calls that the decorator overrides would be returned, and any other method calls would "proxied" through __call to the decorated object.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload