简体   繁体   中英

Symfony + Doctrine 2.2 DateTime column error

I'm having a hard time sorting this issue out. I'm getting the following error while trying to persist an entity (see source below):

Fatal error: Call to a member function format() on a non-object 
in *****\vendor\doctrine-dbal\lib\Doctrine\DBAL\Types\DateTimeTzType.php 
on line 64

Here's a snippet of the entity's code:

namespace ****\Bundle\****Bundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\ORM\Mapping as ORM;

use Symfony\Component\Validator\Constraints as Assert;

/**
 * ****\Bundle\****Bundle\Entity\MyEntity
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="****\Bundle\****Bundle\Entity\****Repository")
 * @ORM\HasLifecycleCallbacks()
 */
class MyEntity
{

/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var \DateTime $created_at
 * 
 * @ORM\Column(name="created_at", type="datetime")
 */
private $created_at;

/**
 * @var \DateTime $updated_at
 * 
 * @ORM\Column(name="updated_at", type="datetime", nullable="true")
 */
private $updated_at;

/**
 * Set created_at
 *
 * @param datetime $createdAt
 */
public function setCreatedAt($createdAt)
{
    $this->created_at = $createdAt;
}

/**
 * Get created_at
 *
 * @return datetime 
 */
public function getCreatedAt()
{
    return $this->created_at;
}

/**
 * Set updated_at
 *
 * @param datetime $updatedAt
 */
public function setUpdatedAt($updatedAt)
{
    $this->updated_at = $updatedAt;
}

/**
 * Get updated_at
 *
 * @return datetime 
 */
public function getUpdatedAt()
{
    return $this->updated_at;
}

/**
 * @ORM\PrePersist()
 */
public function executePrePersist()
{
    $this->created_at = new \DateTime();
}

/**
 * @ORM\PreUpdate()
 */
public function executePreUpdate()
{
    $this->updated_at = new  \DateTime();
}
}

Before posting here, I've added:

print_r(get_class($value))

to DateTimeTzType.php at the offending place to know which kind of data it received, and I got the following error:

Warning: get_class() expects parameter 1 to be object, string given 

So it seems that it is receiving a string instead of a DateTime object, and then fails because string doesn't have a format() method.

I'm using Symfony 2.0.9. Am I missing something?

Seems like your setCreatedAt() and getCreatedAt() do not specify they require DateTime as a type, which Doctrine expects.

You can either make sure you create a DateTime object when calling those functions or modify the methods to check if a string was provided as the input and create the object when needed. Just sending the string representation of the date as the first argument to the DateTime constructor usually works.

It was my error. I had another field with DateTime type elsewhere in my entitiy and I was passing a string to it before persisting. I was convinced it was the createdAt field. Thanks xdebug and sorry for the post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM