简体   繁体   English

Symfony2和Doctrine - 未定义的索引

[英]Symfony2 and Doctrine - Undefined index

I have a Symfony 2.4.x project. 我有一个Symfony 2.4.x项目。

In there, I have two entities that are mapped together: Conference and Paper. 在那里,我有两个映射在一起的实体:会议和论文。

Each conference has papers and with a specific conference, I would like to get the number of papers. 每个会议都有论文和特定会议,我想获得论文数量。

For that, in my conference entity, I have: 为此,在我的会议实体中,我有:

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

In the entity Paper I have: 在实体论文中我有:

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

When I had this project on Symfony2.0, everything worked well, but now I migrated it in Symfony 2.4.x and I am getting the following error when trying to do: 当我在Symfony2.0上有这个项目时,一切都运行良好,但现在我在Symfony 2.4.x中迁移它,我在尝试时遇到以下错误:

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

Error: 错误:

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

EDIT: Here are the full classes of the two entities in pastebin: 编辑:以下是pastebin中两个实体的完整类:

EDIT 2: Here are some news that I found by trying to solve the problem. 编辑2:这是我通过尝试解决问题找到的一些消息。 Another classe is involved there: Submission. 另有一个classe参与其中:提交。

Here is the code: http://pastebin.com/bkdRtjdq 这是代码: http//pastebin.com/bkdRtjdq

In the class Submission, I have the primary key that is hash_key and not the id. 在Submission类中,我有主键是hash_key而不是id。

I had the same error when I was trying to get entities with ObjectManager in ManyToMany realtionship: 当我试图在ManyToMany使用ObjectManager获取实体时,我遇到了同样的错误:

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

My workaround was to write find method in Repository with DQL: 我的解决方法是使用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();
}

Maybe it will be useful for someone. 也许对某人有用。

Are there any cases where papers are 0? 是否存在论文为0的情况? The conf.papers variable may be null and you would need to check if it's null first before counting. conf.papers变量可能为null,您需要在计数之前先检查它是否为null。 {% if conf.papers is empty %} ... {% endif %} {%if conf.papers为空%} ... {%endif%}

I would guess it could be caused by the plural case: paper s 我想这可能由多种情况造成的:纸S

Try this: 尝试这个:

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

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

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