通过 $object->save(); 保存对象属性问题

发布于 2024-10-11 01:18:45 字数 2126 浏览 1 评论 0原文

我是一名 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 的一些内存问题。

但是另外两段代码为什么不起作用?

谢谢, 胡格斯.

I'm a magento programmer and I've been loosing several long minutes to figure out why a property on an object was not saved in the database.

Let's explain, here are 3 pieces of code that I would expect to do the same thing :

First code

$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

No value saved in database.

Second code

$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

No value saved in database.

Third code

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

Finally, I got it !

The code from 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);
    }

note: The final setData() always returns $this

Question, Why ?

I think I've forgot some specificities about the way PHP works, especially for the first code. I would understand that it doesn't work because of some memory stuff with PHP.

But the two other pieces of code, why doesn't it work ?

Thanks,
Hugues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2024-10-18 01:18:45

从 Magento 的角度来看,这些代码片段是相同的 - 您没有忘记 PHP 的工作原理。使用默认的 Magento 安装,所有 3 个片段必须产生相同的结果。

如果这些代码块的结果不同,那么您应该:

a) 关闭您使用的所有自定义扩展,并尝试不使用它们的代码块 - 也许某些扩展会修改订单或付款模型的默认行为。

b)检查您的代码片段是否与本问题中提供的确实相同 - 也许还有其他您认为不重要且未包含在本问题中的代码行

c)检查您是否在之后更新了 MySQL 客户端中的视图执行每个代码片段 - 也许您在付款表中只看到一些旧信息

c2) 检查您是否没有使用复制的 MySQL 服务器 - 也许您更新了主数据库上的信息,但看到了从数据库中的付款表,而这些更改还没有发生尚未同步到

d) 检查在您的代码之后没有其他代码执行 - 也许其他一些模型或控制器修改了additional_information,因此删除了您的所有更改。尝试在代码后面插入“exit”,这样您就可以确定这一点。

These pieces of code are identical from Magento view - you didn't forget anything about how PHP works. With default Magento installation all 3 snippets must produce same results.

If the results of those code blocks are different, then you should:

a) turn off all custom extensions you use and try your code blocks without them - maybe some of extensions modify the default behavior of Order or Payment models.

b) check that your code snippets are really same as presented in this question - maybe there were other code lines that you thought of as non-important and didn't include in this question

c) check that you update view in your MySQL client after executing each code snippet - maybe you see just some old information in payment table

c2) check that you don't use replicated MySQL severs - maybe you update information on master DB, but sees payment table from slave DB, where these changes haven't yet been synced to

d) check that no other code executes after yours - maybe some other model or controller modifies additional_information and so deletes all your changes. Try to insert 'exit' just after your code so you'll be sure about it.

烈酒灼喉 2024-10-18 01:18:45

不是 Magento 用户,但看起来好像每个方法都返回链中下一个方法所需的对象。

如果单独调用每个方法,它们创建或修改的对象将不包含先前方法调用所做的任何更改。通过链接方法调用,每个方法调用都会获取前一个调用所做的更改。

Not a Magento user, but it looks as if each method is returning an object which is required by the next method in the chain.

If you call each method individually, the object they create or modify won't contain any changes made by the previous method calls. By chaining the method calls, each one picks up the changes made by the previous call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文