属性が存在する場合は、製品コレクションフィルタを取得しますか?
質問
目標:特定の属性を持つ製品のコレクションを取得する
これが試みているものです:
public function Hydrate($attribute)
{
$this->id = Mage::helper('myHelper')->CamelCase($attribute->getFrontendLabel());
$this->name = $attribute->getFrontendLabel();
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter($attribute->getAttributeCode(),array('like' => '%%'))
->load();
foreach ($products as $product)
{
array_push($this->collection,Mage::getModel('myModels/Json_Product')->Hydrate($product));
}
return Mage::helper('myHelper')->ConvertToJson($this);
}
.
仕事が働いています。しかし、私は、AddFieldToFilter()が私が現在見ていないどんな再灌流も持っているのか疑問に思っていましたか?
Magento 1.9.x
解決
LIKE
は最も優しい選択ではありません。%%
(%
と同等)は、文字列、さらには空の文字列でも、フィールドがNOT NULL
であるかどうかを確認したいと思っています。
この場合の右側のフィルタパラメータは次のとおりです。
->addFieldToFilter($attribute->getAttributeCode(),array('notnull' => true))
. 他のヒント
これに関する保護された質問を見ることができますあなたが必要なものを実行する方法を表示します。
を使用しています$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');
//filter for products whose orig_price is greater than (gt) 100
$collection->addFieldToFilter(array(
array('attribute'=>'orig_price','gt'=>'100'),
));
.
所属していません magento.stackexchange