【问题标题】:Symfony2 and Doctrine - Undefined indexSymfony2 和 Doctrine - 未定义索引
【发布时间】:2014-02-01 08:40:48
【问题描述】:

我有一个 Symfony 2.4.x 项目。

在那里,我有两个映射在一起的实体:会议和论文。

每个会议都有论文,对于特定的会议,我想知道论文的数量。

为此,在我的会议实体中,我有:

/**
 * @ORM\OneToMany(targetEntity="Paper", mappedBy="conference")
 */
protected $papers;

在我拥有的实体 Paper 中:

/**
 * @ORM\ManyToOne(targetEntity="Conference", inversedBy="papers")
 * @ORM\JoinColumn(name="conference_id", referencedColumnName="id")
 */
protected $conference;

当我在 Symfony2.0 上做这个项目时,一切运行良好,但现在我将它迁移到 Symfony 2.4.x 中,尝试执行此操作时出现以下错误:

count($conf->getPapers()); // In the controller
{{ conf.papers | length }} // In the twig template

错误:

ContextErrorException: Notice: Undefined index: hash_key in /var/www/git/conference2.0-v2.4/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php line 121

编辑:这是 pastebin 中两个实体的完整类:

编辑 2:这是我在尝试解决问题时发现的一些消息。那里涉及另一个类:提交。

这里是代码:http://pastebin.com/bkdRtjdq

在提交类中,我的主键是 hash_key 而不是 id。

【问题讨论】:

  • 你能提供完整的论文和会议课程吗?
  • @Flask,我使用 pastebin 在我的问题中添加了完整代码。感谢您的帮助。
  • 你解决了吗?
  • @Craig,很遗憾没有。
  • 我有一个类似的错误 (ContextErrorException),只有我的错误是在 ObjectHydrator 中抛出的。我最终通过删除注释的“inversedBy”部分来修复它。不知道为什么,但它为我修好了。

标签: php symfony doctrine-orm entity twig


【解决方案1】:

我猜这可能是由复数情况引起的:papers

试试这个:

/**
 * @ORM\OneToMany(targetEntity="Paper", mappedBy="conference")
 */
protected $paper;

【讨论】:

    【解决方案2】:

    当我试图在ManyToMany realtionship 中获取带有ObjectManager 的实体时,我遇到了同样的错误:

    $repository->findBy(
        array('members' = > $user)
    );
    

    我的解决方法是使用 DQL 在 Repository 中编写 find 方法:

    public function findByMember(User $member)
    {
        $qb = $this->createQueryBuilder('g');
    
        $qb->innerJoin('g.members', 'wg')
            ->where($qb->expr()->eq('wg', ':member'))
            ->setParameter('member', $member);
    
        return $qb->getQuery()->getResult();
    }
    

    也许它对某人有用。

    【讨论】:

      【解决方案3】:

      是否有论文为0的情况? conf.papers 变量可能为空,您需要先检查它是否为空,然后再计算。 {% 如果 conf.papers 为空 %} ... {% endif %}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 2023-04-05
        • 2017-03-02
        相关资源
        最近更新 更多