doushe7934 2011-06-01 18:53
浏览 38
已采纳

Kohana 3.1 ORM:将空模型属性值保存为0(零)而不是NULL

I have two models, Product and Product_Methodology. In my product edit view I have a select form field to select one of many methodologies or none (empty first option). In my products table I have a INT(10) methodology_id property that gets saved with the id of the methodology selected from the select form field. Everything worked OK until today that I had to make an adjustment to the system in which since now selecting a methodology can be optional. So I changed the methodology_id field of the products table to allow NULL value and removed the not_empty validation rule on the model.

The problem is that now when I save the model selecting the empty option, instead of the expected NULL, I get a 0 (zero) value.

Any clue on this? Thanks a lot and let me know if it's not so clear.

  • 写回答

4条回答 默认 最新

  • dongxuan2015 2011-06-01 20:58
    关注

    What form input are you using for choosing the methodology? is it <select> ? If so, the value of choosen option is probably set to 0 when no methodology is choosen, and send with other form data.

    In such cases I make a custom filter withing model's filters() method, to set the value to NULL (as PHP treats it) when it's empty(), something like this:

    public function filters()
    {
        return array(
            'methodology_id' => array(
                array('Filter::null', array(':value')),
            ),
        );
    }
    

    Where Filter is helper class with static methods. like...:

    class Kohana_Filter {
    
        public static function null($value)
        {
            return (empty($value) ? NULL : $value);
        }
    
    }
    

    Hope this helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于c++外部库文件宏的问题,求解
  • ¥15 office打开卡退(新电脑重装office系统后)
  • ¥300 FLUENT 火箭发动机燃烧EDC仿真
  • ¥15 【Hadoop 问题】Hadoop编译所遇问题hadoop-common: make failed with error code 2
  • ¥15 vb6.0+webbrowser无法加载某个网页求解
  • ¥15 RPA财务机器人采购付款流程
  • ¥15 计算机图形多边形及三次样条曲线绘制
  • ¥15 根据protues画的图用keil写程序
  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误