我是一名 magento 程序员,我已经浪费了很长时间来弄清楚为什么对象上的属性没有保存在数据库中。
让我们解释一下,这里有 3 段我希望做同样事情的代码:
第一个代码
$order = Mage::getModel('sales/order')->load(1873);
$myInfo = 'important piece of information';
$order->getPayment()->setAdditionalInformation('my_info',$myInfo);
$order->getPayment()->save(); //No information in the database is saved
没有值保存在数据库中。
第二个代码
$order = Mage::getModel('sales/order')->load(1873);
$myInfo = 'important piece of information';
$payment = $order->getPayment();
$payment->setAdditionalInformation('my_info',$myInfo);
$payment->save(); //No information in the database is saved
没有值保存在数据库中。
第三个代码
$order = Mage::getModel('sales/order')->load(1873);
$myInfo = 'important piece of information';
$order->getPayment()->setAdditionalInformation('my_info',$myInfo)->save(); //YEAHHH ! It works ! I now have that in my database.
最后我得到了它 !
来自 setAdditionalInformation 的代码
/**
* Additional information setter
* Updates data inside the 'additional_information' array
* or all 'additional_information' if key is data array
*
* @param string|array $key
* @param mixed $value
* @return Mage_Payment_Model_Info
* @throws Mage_Core_Exception
*/
public function setAdditionalInformation($key, $value = null)
{
if (is_object($value)) {
Mage::throwException(Mage::helper('sales')->__('Payment disallow storing objects.'));
}
$this->_initAdditionalInformation();
if (is_array($key) && is_null($value)) {
$this->_additionalInformation = $key;
} else {
$this->_additionalInformation[$key] = $value;
}
return $this->setData('additional_information', $this->_additionalInformation);
}
注意:最终的 setData() 总是返回 $this
问题,为什么?
我想我已经忘记了 PHP 工作方式的一些细节,尤其是对于第一个代码。我会明白,由于 PHP 的一些内存内容,它不起作用。
但是另外两段代码,为什么它不起作用?
谢谢,休斯。