Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(Didn't mean to leave you hanging in the other thread, but there's not a quick answer to this one that wouldn't either seem like magic or just confuse people further.)</p> <p>You don't need to override a class unless you're going to <strong>change</strong> the behavior of an existing method. You just need to create a new class that <strong>extend</strong> the existing class. Here's how you do that.</p> <h2>Terminology For the Normal State of Things</h2> <ol> <li><p>Models are the logical classes/objects that define the behavior of some "thing" (product, etc.) in Magento</p></li> <li><p>Models contain Model Resources. Model Resources are the classes that do the actual fetching of data from some datastore (mysql, etc). This is the Data Mapper pattern.</p></li> <li><p>Collections are objects with array like properties that query the database and return a group of Models. Somewhat confusingly, Collections are <strong>also</strong> Model Resources. </p></li> </ol> <p>So, in the normal state of things, you might say something like</p> <pre><code>Mage::getModel('catalog/product') </code></pre> <p>to get a product model and the underlying system uses</p> <pre><code>Mage::getResourceModel('catalog/product'); </code></pre> <p>to get the Mode Resource object that queries for a single product, and <strong>either</strong> of the following are used</p> <pre><code>Mage::getResourceModel('catalog/product_collection'); Mage::getModel('catalog/product')-&gt;getCollection(); </code></pre> <p>to get the Collection object that queries for many models. In current versions of Magento each Model object has a method named "getCollection" which returns its corresponding Collection Resource. </p> <h2>Reports Off the Rails</h2> <p>Reports go a little sideways, but everything is still within the same universe as described above. It turns out there's no such model as a </p> <pre><code>Mage::getModel('reports/product'); </code></pre> <p><strong>But</strong> there is a collection</p> <pre><code>Mage::getResourceModel('reports/product_collection') </code></pre> <p>If you look at the class (which you'll be extending below), you'll see that the 'reports/product_collection' collection</p> <pre><code>class Mage_Reports_Model_Mysql4_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection </code></pre> <p><strong>extends the the base product collection</strong> class. In other words, the client programmer working on the reports section had the exact same though you did. "I want to add some methods to to <code>Mage::getModelResource('catalog/product_collection')</code>. They did this by <strong>extending</strong> the base class.</p> <h2>Shut Up, I Just Want this to Work</h2> <p>So, what you really want to do here is </p> <ol> <li><p>Create a new class <code>Samsnamespace_Samscatalog_Model_Mysql4_Product_Collection</code> that extends the base <code>Mage_Reports_Model_Mysql4_Product_Collection</code> collection class. </p></li> <li><p>Ensure that a call to <code>Mage::getModelResource('samscatalog/product_collection')</code> returns an instance of the above class by configurig our module to use Models and Model Resources. </p></li> </ol> <p>We're also going to change you Module structure around a little bit to help ease naming confusion. I'm not a big fan of giving module folders the same names as core modules (i.e. "Catalog"), and the top level folder (after local/) is actually a Namespace, not a module folder. (A namespace may contain many modules)</p> <p>We are not overriding the class. We are configuring a custom module under your namespace to use both Models and Model Resources. We're then defining a model resource that extends an existing PHP class already in the system. Overrides should only be used when you want to <strong>change</strong> the behavior of a particular method call. (Appologies for harping on this, but there's enough general confusion in the communiy about this that it's worth harping on over. and over. and over.)</p> <p>First, we're going to create the module directory structure and files. We'll just need two</p> <pre><code>local/Samsnamespace/Samscatalog/etc/config.xml local/Samsnamespace/Samscatalog/Model/Mysql4/Product/Collection.php </code></pre> <p>(and don't forget to enable the module in app/etc/modules. If you're not sure what that means, <a href="http://alanstorm.com/category/magento" rel="noreferrer">start reading</a>)</p> <p>The <code>Collection.php</code> file should contain </p> <pre><code>&lt;?php class Samsnamespace_Samscatalog_Model_Mysql4_Product_Collection extends Mage_Reports_Model_Mysql4_Product_Collection { /* your custom methods go here*/ } </code></pre> <p>And the config file should contain </p> <pre><code>&lt;config&gt; &lt;modules&gt; &lt;Samsnamespace_Samscatalog&gt; &lt;version&gt;0.1.0&lt;/version&gt; &lt;/Samsnamespace_Samscatalog&gt; &lt;/modules&gt; &lt;global&gt; &lt;models&gt; &lt;samscatalog&gt; &lt;class&gt;Samsnamespace_Samscatalog_Model&lt;/class&gt; &lt;resourceModel&gt;samscatalog_mysql4&lt;/resourceModel&gt; &lt;/samscatalog&gt; &lt;samscatalog_mysql4&gt; &lt;class&gt;Samsnamespace_Samscatalog_Model_Mysql4&lt;/class&gt; &lt;/samscatalog_mysql4&gt; &lt;/models&gt; &lt;/global&gt; &lt;/config&gt; </code></pre> <p>With these files in place and the module enabled, you should be able to call </p> <pre><code>$test = Mage::getResourceModel('samscatalog/product_collection'); var_dump(get_class($test)); </code></pre> <p>and your collection will be returned, and you can add methods to your heart's content.</p> <h2>What's Going On</h2> <p>This is mind bending, so you can stop reading if you want. It's also a rehash of concepts I've <a href="http://alanstorm.com/magento_models_orm" rel="noreferrer">covered elsewhere</a>.</p> <p>When you say</p> <pre><code>Mage::getResourceModel('samscatalog/product_collection'); </code></pre> <p>The underlying mage system codes says "ok, so this resource model"</p> <pre><code>samscatalog/product_collection </code></pre> <p>is part of the </p> <pre><code>samscatalog/product </code></pre> <p>model (not entirely true in this case, but it's what the system thinks).</p> <p>So, since the resource model <code>samscatalog/product_collection</code> is part of the <code>samscatalog/product</code> model, let's look at the config at </p> <pre><code>global/models/samscatalog/resourceModel </code></pre> <p>To get the resource model URI of </p> <pre><code>samscatalog_mysql4 </code></pre> <p>and then let's use that to look at the config at </p> <pre><code>global/models/samscatalog_mysql4/class </code></pre> <p>to get the base classname for all Resource Models that are a part of this Module. This ends up being </p> <pre><code>Samsnamespace_Samscatalog_Model_Mysql4 </code></pre> <p>Which means the <code>samscatalog/product_collection</code> Resource Model is named </p> <pre><code>Samsnamespace_Samscatalog_Model_Mysql4_Product_Collection </code></pre> <p>and then its just Magento's standard auto-load which does a </p> <pre><code>include('Samsnamespace/Samscatalog/Model/Mysql4/Product/Collection.php'); </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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