【问题标题】:Call to a member function format() on a non-object in Symfony2 (Doctrine)在 Symfony2 中的非对象上调用成员函数 format() (Doctrine)
【发布时间】:2015-02-24 07:44:34
【问题描述】:

我的控制器中有一些错误。

我会通过这个配置来配置实体:

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

我的控制器中有这段代码

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

但是,如果我尝试运行这段代码,我有

致命错误:在非对象上调用成员函数 format() /vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php 上线 53

我阅读了很多关于此错误的帖子,但找不到适合我的解决方案。

更新: 一些魔法! O_o 我在DateTimeType.php文件中添加了如下代码

if (!method_exists($value, 'format')) {
  var_dump($value); 
  var_dump(debug_backtrace()); 
} 

魔法从这里开始……到日期字段填充……关于 csrf-token 的信息。 如何?为什么? — 我不知道…… :(

很遗憾,我无法解决问题。我通过生成 SQL 代码并在实体管理器中指导其实现解决了这个问题。

【问题讨论】:

  • 一些魔法! O_o 我将以下代码添加到文件 DateTimeType.php if (!method_exists($value, 'format')) {var_dump($value); var_dump(debug_backtrace()); } 中,魔法开始了……date 字段填充了……关于 csrf-token 的信息。如何?为什么? — 我不知道…… :(
  • 我怀疑你的实体关系都搞砸了。您不应该同时拥有 news_id 和新闻关系。也许用 NewsAuthorHistory 代码更新您的问题。还可以考虑关闭一个新项目,然后按照教义章节的顺序工作,直到您对教义 2 有一个实际的理解。

标签: php symfony datetime doctrine-orm doctrine


【解决方案1】:
$history_event->setDate(\DateTime('now'));

应该是

$history_event->setDate(new \DateTime());

您的错误是您没有实例化您的实体所期望的 DateTime 对象。此外请记住nowDateTime 的默认参数。

编辑

您能在创建一个新的DateTime 之前尝试插入这个sn-p 代码吗?

date_default_timezone_set('America/New_York'); //Just an example to understand what's going on

【讨论】:

  • 是的,我会尝试很多解决方案:$history_event->setDate(new \DateTime('now'));$now = new \DateTime('now'); $history_event->setDate($now);# Entities/NewsAuthorHistory.php class NewsAuthorHistory { public function __construct() { $this->date = new \DateTime('now'); } }
  • @Dmitry:我的解决方案不起作用?我简直不敢相信!
  • 我也不敢相信。
  • @Dmitry 你能检查你的日志错误文件吗?我很确定您的 php 解释器会由于默认时区设置而报告错误
  • 我在这段代码中发现了一点魔力。查看帖子的评论。
【解决方案2】:

根据您的更新,我将 DateTimeType.php 中的 convertToDatabaseValue 修改为:

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

现在可以了。

【讨论】:

    猜你喜欢
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多