Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using insert() method to insert DateTime value with PHP Doctrine\DBAL 2

How can i pass PHP's DateTime object as a value for database field using Doctrine\DBAL?

$DB is a Doctrine\DBAL\Connection instance.

$DB->insert('table_name', [
    'field' => new \DateTime(),
]);

// Catchable fatal error: Object of class DateTime could not be converted to string

The code above is not working and documentation is scarce.

I knew for sure that you can provide DateTime objects directly using another DBAL methods, is it possible to do this with insert()?

like image 650
Slava Fomin II Avatar asked Apr 04 '12 14:04

Slava Fomin II


1 Answers

$DB->insert('table_name', [
    'foo'   => 'foo',
    'bar'   => 17,
    'field' => new \DateTime(),
], [
    PDO::PARAM_STR,
    PDO::PARAM_INT,
    'datetime',
]);

Did the trick! ))

like image 153
Slava Fomin II Avatar answered Oct 21 '22 14:10

Slava Fomin II