属性値の存在を確認します。これは何をしますか?
-
16-10-2019 - |
質問
このコードを取得しました ここ, 、それは機能します。しかし、私は十分に得られないセクションがあります。行[a]および[b]を参照してください。
public function attributeValueExists($attCode, $attributeValue)
{
$attributeModel = Mage::getModel('eav/entity_attribute');
$attributeOptionModel = Mage::getModel('eav/entity_attribute_source_table') ;
$attributeId = $attributeModel->getIdByCode('catalog_product', $attCode); // [A]
$attribute = $attributeModel->load($attributeId); // [B]
$attributeTable = $attributeOptionModel->setAttribute($attribute);
$options = $attributeOptionModel->getAllOptions(false);
foreach($options as $option) {
if ($option['label'] == $attributeValue) {
return $option['value'];
}
}
return false;
}
行[a]および[b]から得られた変数、 $attributeTable
, 、使用されていないようです。しかし、これらの2行をコメントすると、コードは機能しません。これらの線は実際に何をしているのか、なぜそうするのか $attributeTable
にとって?
解決
「使用されていない」と言うと間違っています。実際、彼らは次のとおりです。
この行は$ aTtributeIDを取得します:
$attributeId = $attributeModel->getIdByCode('catalog_product', $attCode); // [A]
この行は属性をロードします:
$attribute = $attributeModel->load($attributeId); // [B]
この行は、$ aTtionTOPTIONMODELのセッターの$属性を使用します。
$attributeTable = $attributeOptionModel->setAttribute($attribute);
getAlloptionsでは、属性が設定されている必要があります。
$options = $attributeOptionModel->getAllOptions(false);
あなたがつまずいているところは、$属性が使用されていないということだと思いますが、それは単なるセッターメソッドからの返品値です。 Magentoのマジックセッターは、常に親オブジェクトを返します 流fluentインターフェイスを構築できるようにします.
他のヒント
この関数は、この属性である属性値を返します。この関数を同様に書くことができます:
public function attributeValueExists($attCode, $attributeValue)
{
$attributeModel = Mage::getModel('eav/entity_attribute');
$attributeOptionModel = Mage::getModel('eav/entity_attribute_source_table') ;
//retrieve attribute_id using attribute_code
$attributeId = $attributeModel->getIdByCode('catalog_product', $attCode); // [A]
//load attribute model using attribute_id
$attribute = $attributeModel->load($attributeId); // [B]
$options = $attributeOptionModel->setAttribute($attribute)->getAllOptions(false);
foreach($options as $option) {
if ($option['label'] == $attributeValue) {
return $option['value'];
}
}
return false;
}
しかし、これは長い道のりであり、このスニペットを使用して属性値を取得できます。
public function attributeValueExists($attCode, $attributeValue)
{
/**
* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract
*/
$attribute = Mage::getModel('catalog/resource_eav_attribute')
->loadByCode(Mage_Catalog_Model_Product::ENTITY, $attCode);
$options = $attribute->getSource()->getAllOptions();
foreach ($options as $option) {
if ($option['label'] == $attributeValue) {
return $option['value'];
}
}
return false;
}
所属していません magento.stackexchange