2

Magento で製品などを更新するために、外部サーバーからの json リクエストを使用しています (API を使用しなかった理由を尋ねないでください...)。属性情報を更新しようとしています。リソース抽象の _uniqueCheck() メソッドが失敗しているようです。問題は、それを修正する方法がわからないことです。次のコード

$attribute_model = Mage::getModel('eav/entity_attribute');
$attributeId = $attribute_model->getIdByCode('catalog_product', $attribute_code);
$attribute = $attribute_model->load($attributeId);

$attribute->setData($data)->save();

次のエラーが発生します。

[29-May-2013 16:32:00 UTC] PHP Fatal error:  Uncaught exception 'Mage_Core_Exception' with message 'Attribute with the same code already exists.' in .../app/Mage.php:594
Stack trace:
#0 .../app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(676): Mage::throwException('Attribute with ...')
#1 .../app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(424): Mage_Core_Model_Resource_Db_Abstract->_checkUnique(Object(Mage_Eav_Model_Entity_Attribute))
#2 .../app/code/core/Mage/Core/Model/Abstract.php(318): Mage_Core_Model_Resource_Db_Abstract->save(Object(Mage_Eav_Model_Entity_Attribute))
#3 .../app/code/local/Valoran/Import/Model/Attribute.php(25): Mage_Core_Model_Abstract->save()
#4 .../app/code/local/Valoran/Harmony/Model/Messaging/Attributeset.php(115): Valoran_Import_Model_Attribute->_create(Array)
#5 .../app/code/local/Valoran/Harmony/Model/Messaging/Attributeset.php(23): Valoran_Harmony_Model_Messaging_Attribut in .../app/Mage.php on line 594

準備が整ったコードがすべて存在することを知っているので、これは私にとってイライラします.->load($id) ....

ここで私が見逃しているものについて何か考えている人はいますか? これを達成するために Mage_Eav_Model_Entity_Setup::updateAttribute メソッドを使用して調査していますが、これまでのところ同じエラーが発生しています。

4

1 に答える 1

1

これが起こっている理由は 2 つ考えられます。どちらもsetData()メソッドの使用に関連しています。配列を引数として呼び出すと$_data、モデルの保護されたプロパティが置き換えられます。このプロパティには、データベースにロード/保存されるすべての情報が含まれます。

あなたの$data変数にはキーがないと思われentity_attribute_idます。呼び出すときにこれが当てはまる場合setData()は、ロードされた属性の id を削除しています。attribute_codeシステム内の別の属性によって既に使用されているものを使用している可能性があります。とにかく、属性を保存するとき、magento は entity_attribute_id をチェックし、空の場合、magento は新しい属性を作成する必要があると想定して、新しい属性の検証を実行します。これにより、不正でエラーをスローする同じコードを持つ属性が検出されます。この属性はロードしたものである可能性がありますが、entity_attribute_idプロパティまたは同じ属性を持つ別のプロパティを削除しましたattribute_code

使いたくない場合は、Mage_Eav_Model_Entity_Setup::updateAttribute電話してみてください

$attribute = $attribute_model->load($attributeId);
$data['entity_attribute_id'] = $attribute->getEntityAttributeId();
$attribute->setData($data)->save();
于 2013-05-29T20:59:05.290 に答える