オブザーバーで製品のコレクションを取得しながらトラブル
-
16-10-2019 - |
質問
私は、いつ呼び出されたときに呼び出されるためにオブザーバーを収容する小さなモジュールを構築しようとしています catalog_product_import_profile_after
解雇されたこれは、私が使用して解雇したカスタムイベントです
Mage::dispatchEvent('catalog_product_import_profile_after',array('adapter'=>$this));
の中に app/code/core/Mage/Adminhtml/controllers/System/Convert/ProfileController.php
ファイル
私のモジュールconfig.xmlはです
<?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>
そしてその Observer.php
は
<?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');
}
}
DisableProducts関数の下でのメールは正しく機能していますが、このコードを使用して製品コレクションを取得できません $collection = Mage::getModel('catalog/product')->getCollection();
私が間違ったことを親切に指摘したことについて何らかの考えを持っている体。ありがとう。
解決
コレクションは、その内部のデータにアクセスするまで空になります(つまり、 foreach
ループ)、または呼び出します ->load()
それに対する方法。コレクションがロードされた後、含まれる結果セットを編集できなくなるため、このアプローチを使用すると、コレクションオブジェクト内に含まれる選択オブジェクトを変更して、返された結果を改良およびフィルタリングできます。これにより、データベースに対するクエリの実行が変更されるため、コレクションがロードされたら、コレクションに表示されるアイテムが変更されます。
代わりに使用する場合
$collection = Mage::getModel('catalog/product')->getCollection()->load();
コレクションにはデータが含まれています。店に依存していることに注意してください catalog/product
コレクションは非常に大きくなる可能性があるため、これは膨大な量のデータである可能性があるため、コンテンツにメールを送信することをお勧めしません。これは特にライブストアの場合です。デバッグしようとしている場合は、Xdebugとローカル開発環境でNetBeansなどのIDEを使用して、オブジェクトとその内容を検査する必要があります。
他のヒント
使用してみてください
$collection = $observer->getProduct();
var_dump($collection);