これらの手法のほとんどを組み合わせて、必要なクエリを作成できるはずです。売上テーブルの場合、あなたはおそらく使用しますaddFieldToFilter
-しかし、Zend_Db_Expr
おそらくあなたのために最も抵抗の少ないパスです。
addAttributeToFilter:
Magento Wikiによると:OR
条件を持つ括弧を作成するとき、次のことができます:
配列が渡されても属性コードが指定されていない場合、同じ方法で処理されるOR条件のグループとして解釈されます。
したがって、それから次を構築できます。
$collection->addAttributeToFilter(
array(
array('attribute'=> 'someattribute','like' => 'value'),
array('attribute'=> 'otherattribute','like' => 'value'),
array('attribute'=> 'anotherattribute','like' => 'value'),
)
);
これWHERE
により、次の形式の句が出力されます。
WHERE ((someattribute LIKE 'value') OR (otherattribute LIKE 'value') OR (anotherattribute LIKE 'value'))
addFieldToFilter:
モデルがDBテーブルに直接リンクされている場合、名前によってデータベース列に条件を適用するには、以下が必要です。
$collection->addFieldToFilter(
array('title', 'content'),
array(
array('like'=>'%$titlesearchtext%'),
array('like'=>'%$contentsearchtext%')
)
)
Zend_Db_Expr:
より複雑な構造の場合、を使用して独自のwhere句を作成できますZend_Db_Expr
。例えば :
$collection->getSelect()->where(new Zend_Db_Expr("(e.created_at > '2013-01-01 00:00:00' OR e.created_at <'2012-01-01 00:00:00)"));
ソース:
/programming/5301231/addattributetofilter-and-or-condition-in-magentos-collection
/programming/3826474/magento-addfieldtofilter-two-fields-match-as-or-not-and/7851884#7851884