We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Multiple date columns in a table problem

Hello,

I have stumbled upon a strange error:

I have a table in MySQL:

CREATE TABLE event ( id int(11) NOT NULL AUTO_INCREMENT, start_date date NOT NULL, end_date date DEFAULT NULL, PRIMARY KEY (id), );

My model looks like that: class Event extends \Phalcon\Mvc\Model {

public $id;
public $start_date;
public $end_date;

public function initialize() {
    $this->setSchema("MyDatabase");
    $this->setSource("event");
}

public function getSource() {
    return 'event';
}

public static function find($parameters = null) {
    return parent::find($parameters);
}

public static function findFirst($parameters = null) {
    return parent::findFirst($parameters);
}

}

When I do:

$event = new Event; $event->start_date = '2018-01-01'; $event->end_date = '2018-02-02';

$event->save();

Nothing is happening - no data inserted into the database I have implemented a listened on the database

$dbListener = new MyDbListener(); $eventsManager->attach('db', $dbListener);

Not even a qury is executed.

When I remove one of the date columns all works fine!

It looks like phalcon does not like two columns with date type in one table.

I would appreciate someone gave me some advice where the problem might be.

I ma trying to avoid to write sql strng (I know it can be done this way) I want to use proper way by using the phalcon models.

Please help!

Regards Rafal

wich error show you this

if (!$event->save()) {
    var_dump($event->getMessages());
}

tell us

edited Jul '18

HI Emilio,

The thing is: it does not rearch that line, it dies inside of the save fumction. When I connect xdebug and stop on

$event->save(); // break poin here 
if (!$event->save()) { // but this line is not reached 
    var_dump($event->getMessages());
}

and then just go over that line, the debug serssion is stopped ant the whole process is finished.

I have updated phalcon to the newest version but I get the same result.

From the db listener I get:

[Sat, 14 Jul 18 17:14:12 +1000][INFO] SELECT IF(COUNT(*) > 0, 1, 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`= 'event' AND `TABLE_SCHEMA` = 'Test'
[Sat, 14 Jul 18 17:14:12 +1000][INFO] DESCRIBE `Test`.`event`

And it ends here

As soon as I remove the second DATE from the model and the table all works fine. Proper insert SQL is executed

Any Ideas?

Regards Rafal



4.7k
edited Jul '18

Did you create the Model php file by yourself?

The Projects (mapped Database Attributes) needs the Annotations. I think the Model Engine needs these Annotations to know which fields must be saved to the database and which are simple object properties.

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(column="id", type="integer", length=11, nullable=false)
     */
    public $id;

    /**
     *
     * @var string
     * @Column(column="start_date", type="string", nullable=true)
     */
    public $start_date;

    /**
     *
     * @var string
     * @Column(column="end_date", type="string", nullable=true)
     */
    public $end_date;

I'm writing type="string" because Phalcon maps DATE and DATETIME as strings internal.

Try using the Phalcon devtools to create the model automatically for specific database tables

edited Jul '18

@mikel date and datetime are supported like valid types docs

@threelight which Phalcon, OS, PHP, etc version are using?