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 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services