3

使用しているmagentoの1つのフィールド値を更新したい

$data = array('xxx'=>$xxx);
$orderModel = Mage::getModel('sales/order')->load($orderId)->addData($data);
try {
    $orderModel->setId($orderId)->save();
    echo "Data updated successfully.";
} catch (Exception $e){
    echo $e->getMessage();
}

しかし、それは機能していません。データの更新で問題を作成していることを誰でも提案できます。

4

2 に答える 2

1

あなたはMAGIC機能を行うことができます、getそしてset機能

直接試してください:

// if your attribute is column named is_active then you can getIsActive() or setIsActive(1)
$orderModel = Mage::getModel('sales/order')->load($orderId)->setYourAttribute($data);
// it means you can set and get column named by ['your_attribute']
try {
    $orderModel->setId($orderId)->save();
    echo "Data updated successfully.";
} catch (Exception $e){
    echo $e->getMessage();
}

うーん。販売/注文モデルのentity_idを設定した理由がわかりません。ただし、entity_idは別のテーブルの外部キーになる可能性があるため、機能しません。例:sales_flat_order_payment

于 2012-09-03T14:26:36.030 に答える
1

コードは、明示的に注文を設定する(つまり、注文を作成する) ことをsetId($orderId)望んでいるように見えます。または、指定された注文を更新するだけの場合、ロード後に必要ないという事実に気付いていないだけのようです。 .entity_idsetId($orderId)

注文を作成しようとしている場合:sales/orderモデルは通常、デフォルトで自動インクリメントされる主キーを使用するため、注文の を明示的に設定することを許可しません。entity_id

既存の注文を更新しようとしている場合: をチェーンsetId($orderId)から削除しsave()ます。

次に、その値をデータベースに保存できるようにする場合は、まず属性を使用してsales/orderモデルを拡張する必要があります。xxx

sales/orderカスタム属性を持つようにモデルを拡張するには、いくつかの方法があります。たとえば、app/code/local/Mycompany/Mymodule/sql/myresource/フォルダーに独自のセットアップ スクリプトを含めることができます。

// Mage_Eav_Model_Entity_Setup
$oInstaller = $this;

// Add EAV attribute
$oInstaller->startSetup();
$oInstaller->addAttribute(
    'order',
    'my_attribute',
    array(
        'type' => 'int'
    )
);
$oInstaller->endSetup();

// Add flat attribute
if (Mage::getVersion() >= 1.1) {
    $oInstaller->startSetup();
    $oConnection = $oInstaller->getConnection();
    $oConnection->addColumn(
        $oInstaller->getTable('sales_flat_order'),
        'my_attribute',
        'TINYINT(1) NOT NULL'
    );
    $oInstaller->endSetup();
}
于 2012-09-03T14:13:19.427 に答える