Verifica dell'esistenza valore dell'attributo. Cosa fa questo?
-
16-10-2019 - |
Domanda
Ho ottenuto questo codice da qui , e funziona. C'è una sezione di esso che non ho ben capito, però. Si prega di vedere linee [A] e [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;
}
La variabile ottenuto da linee [A] e [B], $attributeTable
, non sembra essere usato. Eppure, se io commento queste due righe fuori, il codice non funziona. Quali sono queste linee effettivamente facendo e perché lo fanno $attributeTable
per?
Soluzione
Ti sbagli quando dici che stanno "non utilizzati". Essi sono, infatti:
Questa riga recupera un $ AttributeID:
$attributeId = $attributeModel->getIdByCode('catalog_product', $attCode); // [A]
Questa riga carica l'attributo:
$attribute = $attributeModel->load($attributeId); // [B]
Questa linea utilizza l'attributo $ in un setter sul $ attributeOptionModel:
$attributeTable = $attributeOptionModel->setAttribute($attribute);
getAllOptions richiede che l'attributo sono stati impostati:
$options = $attributeOptionModel->getAllOptions(false);
Credo che dove stai ottenendo inciampato su è che $ attributeTable non viene utilizzato, ma questo è solo il valore restituito dal metodo setter. setter magici in Magento restituiscono sempre l'oggetto padre modo che si può costruire un'interfaccia fluida .
Altri suggerimenti
Questa funzione restituisce valore di attributo che è questo attributo è selezionare o multipla selezionare. Siamo in grado di scrivere questa funzione simile:
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;
}
Ma questo è molto lontano e siamo in grado di utilizzare questo frammento per recuperare valore di attributo:
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;
}