質問

次のコードを使用して、「ドロップダウン属性」にオプションを追加します。

public function createAttributeOption($option){
    $arg_attribute = 'attribute_code';
    $arg_value = $option;

    $attr_model = Mage::getModel('catalog/resource_eav_attribute');
    $attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
    $attr_id = $attr->getAttributeId();

    $option['attribute_id'] = $attr_id;
    $option['value']['any_option_name'][0] = $arg_value;

    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $setup->addAttributeOption($option);
    return '???';
}

そして、それは私の属性にオプションを完全に挿入します。問題は、この関数から挿入されたオプションのIDを返す必要があることです。この最後の挿入オプションのIDを取得するにはどうすればよいですか?

役に立ちましたか?

解決

これを試して:

$lastId = $setup->getConnection()->lastInsertId();
$attr = Mage::getModel('eav/entity_attribute_option')
            ->getCollection()
            ->setStoreFilter()
            ->addFieldToFilter('tsv.value_id',array('eq'=>$lastId))
            ->getFirstItem();

$optionId = $attr->getData('option_id');
return $optionId;

他のヒント

echo $order->getId();
echo $lastInsertId  = $db_write ->fetchOne('SELECT last_insert_id()');
echo $db_write ->lastInsertId(); // you may also try this.

最後の挿入IDに関するすべての情報を取得できますここをクリック

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top