dpyoh6553 2016-12-14 15:11
浏览 31
已采纳

idiorm / paris - 将null写入数据库

I'm using Idiorm with Paris for my PHP project.
I want to add some entrys with some null-values to my mysql database.
For Example:

$openingtime->setBegin(null);
$openingtime->setEnd(null);
$openingtime->setDayOfWeek(1);
$openingtime->save();

The begin- and end-column in database have the type "time" and they are nullable.
The excepted Result

+----+-------+------+-----------+
| id | begin | end  | dayOfWeek |
+----+-------+------+-----------+
|  1 | null  | null |         1 |
+----+-------+------+-----------+

The result I got:

+----+----------+----------+-----------+
| id |  begin   |   end    | dayOfWeek |
+----+----------+----------+-----------+
|  1 | 00:00:00 | 00:00:00 |         1 |
+----+----------+----------+-----------+

ORM::get_last_query() says something like this:

UPDATE `openingtime` SET `begin` = '', `end` = '', `dayOfWeek` = '1'

So instead of null, idiorm/paris inserts an empty string.

Is there a possibility to add null instead of an empty string? Thanks for your Help!

EDIT: Classdefinition of OpeningTime added below

class OpeningTime extends Model {

    public static $_table     = 'openingtime';
    public static $_id_column = 'id';  

    public function getId(){
        return $this->id;
    }
    public function getDayOfWeek(){
        return $this->dayOfWeek;
    }
    public function getBegin(){
        return $this->begin;
    }
    public function getEnd(){
        return $this->end;
    }

    public function setBegin($begin){
        $this->begin = htmlentities( strip_tags($begin), ENT_QUOTES);
    }
    public function setEnd($end){
        $this->end = htmlentities( strip_tags($end), ENT_QUOTES);
    }
    public function setDayOfWeek($dayOfWeek){
        $this->dayOfWeek = htmlentities( strip_tags($dayOfWeek), ENT_QUOTES);
    }
}
  • 写回答

1条回答 默认 最新

  • duandu8202 2016-12-16 15:36
    关注

    The problem were the setters, more precisely the functions htmlentities() and strip_tags() I used in my setters. These functions return an empty String if the given value is null.
    My solution now:

    public function setBegin($begin){
        if(isset($begin)){
            $this->begin = htmlentities( strip_tags($begin), ENT_QUOTES);
        }
        else{
            $this->begin = null;
        }
    }
    

    Thanks!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥50 vue-codemirror如何对指定行 指定位置的 字符进行背景颜色或者字体颜色的修改?
    • ¥15 有人会思科模拟器嘛?
    • ¥30 遇到一个的问题,请教各位
    • ¥20 matlab报错,vflux计算潜流通量
    • ¥15 我该如何实现鼠标按下GUI按钮时就执行按钮里面的操作的方法
    • ¥15 关于#硬件工程#的问题:我这边有个锁相环电路没有效果
    • ¥15 20款 27寸imac苹果一体机装win10后,蓝牙耳机和音响放歌曲卡顿断断续续.
    • ¥15 VB.NET 父窗体调取子窗体报错
    • ¥15 python海龟作图如何改代码使其最后画出来的是一个镜像翻转的图形
    • ¥15 我不明白为什么c#微软的官方api浏览器为什么不支持函数说明的检索,有支持检索函数说明的工具吗?