无法将空字符串保存到字段值中 [英] Can't save empty string into field value

查看:79
本文介绍了无法将空字符串保存到字段值中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将NULL字段的值更改为空字符串(在sales_flat_order表中). 我尝试了这段代码:

I need to change value of NULL-field to empty string (in sales_flat_order table). I tried this code:

$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('created_at', array('to' => $endDate))
    ->addAttributeToFilter('status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE));

foreach ($orders as $order) {
    $comment  = $order->getCustomerNote();
    if (is_null($comment)) {
        $order->setData('customer_note', '');
        try {
            $order->save();
        } catch (Exception $e){
            echo $e->getMessage();  exit;
        }
    }
}

但是在基础上,我仍然看到此字段中的值为NULL. 如果我将空字符串更改为任何非空,则可以正常工作. 如何将NULL值字段更新为空字符串?

But in base I see still NULL value in this filed. If I change empty string to any non-empty it works fine. How can I update NULL value field to empty string?

谢谢.

推荐答案

空字符串在Varien_Db_Adapter_Pdo_Mysql::prepareColumnValue()中设置为 null :

case 'varchar':
case 'mediumtext':
case 'text':
case 'longtext':
    $value  = (string)$value;
    if ($column['NULLABLE'] && $value == '') {
        $value = null;
    }
    break;

要将列值重置为空字符串,请将其添加到资源模型中:

To reset the column value to empty string, add this in your resource model:

protected function _prepareDataForSave(Mage_Core_Model_Abstract $object)
{
    $data = parent::_prepareDataForSave($object);
    if ($object->getData('your_column_name') === '') {
        $data['your_column_name'] = '';
    }
    return $data;
}

这篇关于无法将空字符串保存到字段值中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆