문제

I use the following code to add an option to my "dropdown attribute":

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 '???';
}

and that perfectly inserts the option to my attribute. The question is I need to return the inserted option's ID from this function. How can I get the id of this last inserted option?

도움이 되었습니까?

해결책

Try this:

$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.

You can get all info regarding Last insert id Click Here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top