繁体   English   中英

Symfony + Doctrine 2.2 DateTime列错误

[英]Symfony + Doctrine 2.2 DateTime column error

我很难解决这个问题。 尝试保留实体时出现以下错误(请参见下面的源代码):

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

这是实体代码的片段:

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();
}
}

在这里发布之前,我已经添加:

print_r(get_class($value))

到有问题的地方的DateTimeTzType.php知道它接收了哪种数据,并且出现以下错误:

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

因此,似乎它在接收字符串而不是DateTime对象,然后失败,因为字符串没有format()方法。

我正在使用Symfony 2.0.9。 我想念什么吗?

好像您的setCreatedAt()和getCreatedAt()并未指定它们需要DateTime作为类型,这是Doctrine期望的。

您可以确保在调用这些函数时创建DateTime对象,也可以修改方法以检查是否提供了字符串作为输入,并在需要时创建对象。 通常只将日期的字符串表示形式作为第一个参数发送给DateTime构造函数即可。

是我的错 我在实体中的其他地方有另一个具有DateTime类型的字段,并且在持久化之前将字符串传递给它。 我确信这是createdAt字段。 感谢xdebug,对不起,这篇文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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