MMM
MMM

Reputation: 1

idiorm/paris - write null to database

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

Upvotes: 0

Views: 247

Answers (1)

MMM
MMM

Reputation: 1

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!

Upvotes: 0

Related Questions