1

以下のコードは、製品コレクションを作成してフィルタリングしようとしていますが、正確な製品数が得られないため、理由がわかりません。

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('*')
        ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
        ->addFieldToFilter(array(
                array('attribute'=>'distributor','in'=>array(intval($distributor))),

        ))
        ->addAttributeToFilter('category_id', array(
                array('finset' => strval($cat->getId())),
        ));
$count = $collection->getSize();

たとえば、そのディストリビューターフィルターに約30の商品があるカテゴリがありますが、上記のコードのカウントは20を示しています。3つの商品があるがそのカウントが2に戻る別のカテゴリについても同じです。

更新:代わりにfinsetを使用して自分で問題を修正しました。以下の私の答えを参照してください

4

2 に答える 2

1

このコレクションからクエリコードを取得して、データベースで直接実行してみてください。そして、クエリが正しいかどうかを確認できます。

これを試して:

$collection->getSelect()->__toString();
于 2012-08-13T16:49:06.273 に答える
1

私自身の質問に答えました。マルチセレクトなのでディストリビューターにフィンセットを使用。

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

                    $dists = array(
                      array(
                        "finset" => array(intval($distributor))
                      ),
                    );

                    $collection->addAttributeToSelect('*')
                    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left outer')
                    ->addAttributeToFilter("distributor", $dists)
                    ->addAttributeToFilter('category_id', array(
                         array('finset' => $cat->getId()),
                    ));
                    $collection->load();
                    $count = $collection->getSize();
于 2012-08-13T17:42:30.253 に答える