0

我在我的 PHP 项目中使用 Idiorm 和 Paris。
我想在我的 mysql 数据库中添加一些带有一些空值的条目。
例如:

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

数据库中的开始列和结束列具有“时间”类型,并且它们可以为空。
例外结果

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

我得到的结果:

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

ORM::get_last_query() 是这样说的:

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

因此,idiorm/paris 插入了一个空字符串,而不是 null。

是否有可能添加 null 而不是空字符串?感谢您的帮助!

编辑:下面添加了OpeningTime的类定义

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);
    }
}
4

1 回答 1

0

问题在于设置器,更准确地说是我在设置器中使用的函数htmlentities()strip_tags() 。如果给定值为 null,这些函数将返回一个空字符串。
我现在的解决方案:

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

谢谢!

于 2016-12-16T15:36:47.093 回答