2

顧客モデル全体を実際にロードせずに顧客を更新したいと考えています。これが私の現在のコードです:

$customer = Mage::getModel('customer/customer')->load($customerId, 'entity_id');
$customer->setEmail('test@email.com');
$customer->save();

最初にモデルをロードせずにモデルを更新することはできますか?

4

1 に答える 1

7

以下のコードは、モデルのIDが定義されている限り正常に機能するはずですが、オブジェクトが持っていた以前のデータは失われます。

入れる

$customer = Mage::getModel('customer/customer');
$customer->setEmail('test@email.com');
$customer->save();
// will create a customer with an email set to `test@email.com`
// everything else will either be default or null

水分補給による更新

$customer = Mage::getModel('customer/customer')->load($customerId, 'entity_id');
// this step is also known as `hydration` because the model is like
// a sponge in the watter, it sucks in the values
$customer->setEmail('test@email.com');
$customer->save();
// will update a customer and only ovewrite its email to `test@email.com`
// everything else will be as it was before the save

水分補給なしで更新

$customer = Mage::getModel('customer/customer');
$customer->setId($customerId);
$customer->setEmail('test@email.com');
$customer->save();
// will replace all of the values present on the initial customer with
// an email set to `test@email.com`and everything else set to be default or null

単一属性の更新

原則は、entity_id、attribute_code / attribute_id、および値を指定することで属性値を設定できるという事実です。

/* still looking for a usage snippet */

/* defined in `Mage_Eav_Model_Entity_Abstract` */
protected function _setAttributeValue($object, $valueRow)
{
    $attribute = $this->getAttribute($valueRow['attribute_id']);
    if($attribute) {
        $attributeCode = $attribute->getAttributeCode();
        $object->setData($attributeCode, $valueRow['value']);
        $attribute->getBackend()->setEntityValueId($object, $valueRow['value_id']);
    }

    return $this;
}

これには明らかに前述の悪影響はありません。

于 2013-01-16T19:24:03.690 に答える