0

I have the data contains this date i want insert in Mysql by script Zend and Doctrine i get this message Error:

Fatal error: Call to a member function format() on a non-object in C:\wamp\www\imprimvert\vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\DateTimeType.php on line 53

'dateCreation' => string '2013-10-24 10:01:03' 'firstDateAssignement' => string ''

how can i resolved that ?

Thanks in advance

mari roza
  • 27
  • 2
  • 7
  • Possible [duplicate question](http://stackoverflow.com/questions/3378748/doctrine-2-call-to-a-member-function-format-on-a-non-object-in-datetimety) – AlexP Mar 29 '16 at 13:43
  • Can you provide some code pls? – Vasil Dakov Mar 29 '16 at 14:07
  • @Dakov i used in my controller $data ['dateCreation'] = $info ['date_creation'] – mari roza Mar 29 '16 at 15:12
  • 1
    @mariroza You should be using `DateTime` objects for datetime fields (the answer is in the link I posted) i.e `new \DateTime($data['dateCreation']);`. – AlexP Mar 29 '16 at 20:10

1 Answers1

2

As @AlexP already suggested, you can use DateTime object:

<?php
$entity->setDateCreation(new \DateTime($data['dateCreation']));

Personally I am using DoctrineModule that provides a Hydrator to convert an array of data to an object, which is much more elegant:

<?php
$data['dateCreation'];

$hydrator = new DoctrineHydrator($em);

$entity = $hydrator->hydrate($data, new Entity);

$em->persist($entity);
Vasil Dakov
  • 2,040
  • 2
  • 19
  • 38
  • @Dakov i have always Error because some times i have dateCreation 2204 only and other time i have 24/02/2007 and other time i have this :2013-10-24 09:57:49 how can i resolved this issue Thanks in advance – mari roza Mar 31 '16 at 11:31
  • Well, you can use: $date = DateTime::createFromFormat('Y-m-d H:i:s', '2013-10-24 09:57:49'); or $date = DateTime::createFromFormat('d/m/Y', '24/02/2007'); – Vasil Dakov Mar 31 '16 at 11:49