Question

i am trying building a small module which house a Observer for getting called when and event called when catalog_product_import_profile_after is fired this is custom event which i have fired using the

Mage::dispatchEvent('catalog_product_import_profile_after',array('adapter'=>$this));

in the app/code/core/Mage/Adminhtml/controllers/System/Convert/ProfileController.php file

My module config.xml is

<?xml version="1.0"?>
<config>
  <modules>
    <GWB_ClearOrphan>
      <version>0.1.0</version>
    </GWB_ClearOrphan>
  </modules>
  <global>
    <helpers>
      <clearorphan>
        <class>GWB_ClearOrphan_Helper</class>
      </clearorphan>
    </helpers>
    <models>
      <clearorphan>
        <class>GWB_ClearOrphan_Model</class>
        <resourceModel>clearorphan_mysql4</resourceModel>
      </clearorphan>
    </models>
    <events>
      <catalog_product_import_profile_after> <!-- identifier of the event we want to catch -->
        <observers>
        <catalog_product_import_profile_after_handler> <!-- identifier of the event handler -->
        <type>model</type> <!-- class method call type; valid are model, object and singleton -->
        <class>clearorphan/observer</class> <!-- observers class alias -->
        <method>disableProducts</method>  <!-- observer's method to be called -->
        <args></args> <!-- additional arguments passed to observer -->
            </catalog_product_import_profile_after_handler>
        </observers>
      </catalog_product_import_profile_after>
    </events>
  </global>
</config>

and the Observer.php is

<?php
class GWB_ClearOrphan_Model_Observer
{

        public function disableProducts(Varien_Event_Observer $observer)
        {
            //mail('kapil_gupta@gowebbaby.com','sub-test','msg-test');

            try{    
                $collection = Mage::getModel('catalog/product')->getCollection();

                $pro_collection = var_dump($collection);
                mail('kapil_gupta@gowebbaby.com','collection',$pro_collection);

            }
            catch(Exception $e) {
                //Mage::log($e->getMessage(), null, 'collection.log');
            }

 }

the mail under the disableProducts function is working correctly but i am not able to get the product collection using this code $collection = Mage::getModel('catalog/product')->getCollection();

any body having any idea about what i have made wrong kindly point it out. Thanks.

Was it helpful?

Solution

A collection will be empty until you access the data inside it (i.e. foreach loop), or call the ->load() method against it. After a collection has been loaded you can no longer edit the result set it contains, so this approach allows you to refine and filter the returned results by altering the select object contained inside the collection object. This in turn modifies the query run against the database and thus items you will find in the collection once it has been loaded.

If you instead use

$collection = Mage::getModel('catalog/product')->getCollection()->load();

you will find the collection then contains data. Bear in mind that dependant on the store, the catalog/product collection can be very large so I certainly wouldn't recommend emailing it's contents as this could be a huge amount of data - this is especially the case for a live store. If you are attempting to debug you should be using xdebug and an IDE such as netbeans in a local development environment to inspect objects and their contents.

OTHER TIPS

try to use

$collection = $observer->getProduct(); 
var_dump($collection);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top