[英]How can I use addAttributeToFilter to search in Magento for a multi-select value in db?
I have values in database in array and I want to search values also from an array. 我在数组中的数据库中有值,我也想从数组中搜索值。
For example : 例如 :
I have an array ('13,14')
and in database I have values like (13,14)
or (13)
or (14)
or (15)
. 我有一个数组('13,14')
,在数据库中我有(13,14)
或(13)
或(14)
或(15)
。 So when I search ('13,14')
results should appear with values containing at least one of them. 因此,当我搜索('13,14')
结果应显示至少包含其中之一的值。
I'm assuming you're using a collection, as addAttributeToFilter()
is a collection method. 我假设您正在使用一个集合,因为addAttributeToFilter()
是一个集合方法。 Here's how your filter your collection with a set of possibilities, for a given attribute : 这是您针对给定属性使用一组可能性过滤集合的方法:
$myCollection->addAttributeToFilter('id', array('in' => array('13','14')));
'id'
being the name of your attribute. 'id'
是属性的名称。 You can also use the operator nin
instead of in
, to specify a set of values you don't want your attribute value to be in. Check the other operators here : https://fishpig.co.uk/magento/tutorials/addattributetofilter/ 您也可以使用运算符nin
代替in
,以指定一组您不希望属性值包含在其中的值。在此处检查其他运算符: https : //fishpig.co.uk/magento/tutorials/addattributetofilter /
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.