Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony doctrine type datetime expected format y-m-d h:i:s

I have a problem in symfony when I try to select some items from database with doctrine.

I run this:

$allOrders = $em->getRepository('MyBundle:Dieta')->findBy(array('diet_status'=>'active'));
var_dump($allOrders);

And I get the following error:

Could not convert database value "2014-03-03" to Doctrine Type datetime. Expected format: Y-m-d H:i:s

When I insert the date in the database i do it like this:

$format = 'd/m/Y';
$date = \DateTime::createFromFormat($format, $_POST['dietStartDate']);
$dietStartDate = $date->format('Y-m-d H:i:s');
$dietStartDate = new \Datetime($dietStartDate);

$timestamp = strtotime(str_replace('/', '-', $_POST['dietStartDate']));
$timestamp7 = strtotime('+7 days', $timestamp);
$datetime = date("Y-m-d H:i:s", $timestamp7);
$dietDateEnd = new \Datetime($datetime);

The $dietStartDate and $dietDateEnd are inserted in a datetime column in database. In the doctrine.yml I have it also declared as datetime.

Any ideas ?

like image 631
irimie andrei Avatar asked Mar 12 '14 08:03

irimie andrei


1 Answers

From the value "2014-03-03", it seems that your database column type is date but not datetime.

Run php app/console doctrine:schema:update to check whether there are changes.

like image 190
xdazz Avatar answered Nov 02 '22 22:11

xdazz