0

長い週末でしたが、脳が機能していません。複数のタグで商品コレクションをフィルタリングしようとしています。'tag / product_collection'では、'addTagFilter'に1つの値しか指定できないため、where()に独自のステートメントを渡そうとしています。

relation.tag_id = 6 AND relation.tag_id = 9

ただし、タグ6と9の両方を持つ製品が6つある場合でも、クエリをIN(6,9)に変更すると、ANDを削除して、たとえば6または9を選択すると、どちらかを持つすべての製品が返されます。正常に動作しますが、私の人生では、なぜANDが戻ってこないのか理解できません。以下の完全なコード。

$collection = Mage::getResourceModel('tag/product_collection');
$collection->addAttributeToFilter('status', array('eq' => 1));
$collection->addAttributeToFilter('type_id','simple');
$collection->addAttributeToSelect('sku')
    ->addAttributeToSelect('name')
    ->getSelect()->where("relation.tag_id = 6 AND relation.tag_id = 9","");  

前もって感謝します。

===編集===さて、コレクションでこれを行う「SQL」の方法を試してみるのはあまり運がなかったので、誰かが見つけた場合は、以下にリストしてください。全員が賛成します。それからシズ。その間、私はそれを行うための少し厄介な方法を持っています。1000の製品がある場合、これはお勧めしません。少し遅いと想像できます。

// Load our collection
$collection = Mage::getResourceModel('tag/product_collection');
$collection->addAttributeToFilter('status', array('eq' => 1));
$collection->addAttributeToFilter('type_id','simple');
$collection->addAttributeToSelect('sku')
->addAttributeToSelect('name');

// Loop the collection  
foreach ( $collection as $_product)
    {   
    // Load ALL tags for product
    $model = Mage::getModel('tag/tag');
    $tags = $model->getResourceCollection()
        ->addPopularity()
        ->addStatusFilter($model->getApprovedStatus())
        ->addProductFilter($_product->getId())
        ->setFlag('relation', true)
        ->addStoreFilter(Mage::app()->getStore()->getId())
        ->setActiveFilter()
        ->load();

    // loop through all the tags if we find one set a flag to skip returning this product.
    $arr = array();
    foreach($tags as $_t){ $arr[$_t->getId()] = $_t->getName(); }

    $filter_tags = count($_REQUEST['tags']) > 0 ? $_REQUEST['tags'] : array();
    $tag_found = 0;
    $on_filter =  count($filter_tags) > 0 ? 1 : 0;

    foreach($arr as $key => $value){ array_key_exists($key,$filter_tags) ? $tag_found++ : 0; }

    // if all if good send the product to an array to be returned
    if($on_filter && $tag_found){ // add $_product to a return }

}
4

1 に答える 1

0

このコードをチェックアウト

$collection = Mage::getResourceModel('tag/product_collection');
$collection->addAttributeToFilter('status', array('eq' => 1));
$collection->addAttributeToFilter('type_id','simple');
$collection->addAttributeToSelect('sku')
    ->addAttributeToSelect('name')
    ->getSelect()->Where('relation.tag_id=?', 6)
        ->Where('relation.tag_id=?', 9);

複数の where クラスを配置できます。デフォルトでは、を追加します。必要に応じて、の"AND"代わりに"OR"使用できますorWhereWhere

于 2013-01-22T17:58:37.873 に答える