Reputation: 570
I have the some bug in my controller.
I'd config the entity by this config:
pv\MyBundle\Entity\NewsAuthorHistory:
type: entity
table: news_authors_history
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
column: id
generator:
strategy: AUTO
date:
type: datetime
nullable: true
column: date
author_id:
type: integer
unsigned: true
column: author_id
nullable: false
news_id:
type: integer
unsigned: true
column: news_id
nullable: false
manyToOne:
news:
targetEntity: pv\MyBundle\Entity\News
joinColumn:
name: news_id
referencedColumnName: id
author:
targetEntity: pv\MyBundle\Entity\Authors
joinColumn:
name: author_id
referencedColumnName: id
I have this code in my controller
$history_event = new NewsAuthorHistory();
$history_event->setAuthorId($author->getId());
$history_event->setNewsId($news->getId());
$history_event->setDate(\DateTime('now'));
$em->persist($history_event);
$em->flush();
But, If I try to run this code, I have the the
Fatal error: Call to a member function format() on a non-object in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php on line 53
I'd read a lot of posts about this error, but could not find a suitable solution for me.
UPDATE: Some magic! O_o I added the following code to the file DateTimeType.php
if (!method_exists($value, 'format')) {
var_dump($value);
var_dump(debug_backtrace());
}
And here the magic begins… To the date field filled… with information about csrf-token. How? Why? — I don't know… :(
Unfortunately, I could not resolve the problem. I solved this problem by SQL-code generation and directing its implementation in the entity-manager.
Upvotes: 1
Views: 4898
Reputation: 1
following your update I modified my convertToDatabaseValue in DateTimeType.php to this:
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if(is_array($value)){
$value = date_create($value['year'].'-'.$value['month'].'-'.$value['day']);
}
return ($value !== null)
? $value->format($platform->getDateTimeFormatString()) : null;
}
Now it works.
Upvotes: 0
Reputation: 29912
$history_event->setDate(\DateTime('now'));
should be
$history_event->setDate(new \DateTime());
Your error is that you're not instantiating a DateTime
object that is what your entity expects. Moreover remember that now
is default argument for DateTime
.
Could you try, just before creating a new DateTime
to insert this snippet of code?
date_default_timezone_set('America/New_York'); //Just an example to understand what's going on
Upvotes: 3