質問

そこで、ダッシュボード上の管理コードから引っ張ったこのリソースモデルがあります。それは機能しますが、私はそれに参加することはできません。やっている ->getSelect()->__toString(); 何も示していませんか?それで、これは通常のコレクションではありませんか?なぜ他のコレクションのように機能しないのですか?

私が達成したいことを達成するためには、コレクションモデルを拡張する必要があるということですが、ここで何が起こっているのかをもっと理解できるかどうかを確認したかったのです。

$storeId = $this->helper('core')->getStoreId();
$collection = Mage::getResourceModel('sales/report_bestsellers_collection')
    ->setModel('catalog/product')
    ->addStoreFilter($storeId);

$collection->getSelect()->join(
        array('category_product' => 'catalog_category_product'),
        'main_table.product_id = category_product.product_id', 
        array('category_product.*')
    )->where('category_product.category_id = ?', '3')
     ->order('main_table.qty_ordered DESC')
     ->limit($limit);
役に立ちましたか?

解決

レポートコレクションは、それを少し異なって機能します Mage_Core_Model_Abstract/CRUDコレクション。 CRUDコレクションには、インスタンス化時にすぐにSQLクエリが割り当てられます。ただし、レポートコレクションは、負荷操作中にSQLステートメントを初期化します。これをで見ることができます load すべてのレポートコレクションの抽象的なベースクラスのメソッド。

#File: app/code/core/Mage/Reports/Model/Resource/Report/Collection/Abstract.php
public function load($printQuery = false, $logQuery = false)
{
    if ($this->isLoaded()) {
        return $this;
    }
    $this->_initSelect();
    if ($this->_applyFilters) {
        $this->_applyDateRangeFilter();
        $this->_applyStoresFilter();
        $this->_applyCustomFilter();
    }
    return parent::load($printQuery, $logQuery);
}

まで選択はありません _initSelect 呼ばれています。

ここで取りたい一般的なアプローチはそうです

  1. 拡張する新しいリソースモデルクラスを作成します Mage_Reports_Model_Resource_Report_Collection_Abstract クラス

  2. aを定義します _applyCustomFilter クラスの方法

そして、新しいリソースモデルを使用します。あなたの _applyCustomFilter 方法はこのようなものになります。

protected function _applyCustomFilter()
{
    $return = parent::_applyCustomFilter();
    //manipulate the select all you want here (`$this->getSelect()`)
    return $return;
}

他のヒント

「ベストセラー」製品のリストをロードしようとしているように見えます(および特定のカテゴリでフィルタリングされています)、レポートモジュールの製品コレクションを使用して過去にそれをやったことがあります。

これを試してみてください:

$category = Mage::getModel('catalog/category')->load(3);
$report = Mage::getResourceModel('reports/product_collection')
        ->addAttributeToSelect('*')
        ->addOrderedQty()
        ->setOrder('ordered_qty', 'desc')
        ->addCategoryFilter($category);
ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top