简体   繁体   中英

Symfony2 and Doctrine - Undefined index

I have a Symfony 2.4.x project.

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:

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:

EDIT 2: Here are some news that I found by trying to solve the problem. Another classe is involved there: Submission.

Here is the code: http://pastebin.com/bkdRtjdq

In the class Submission, I have the primary key that is hash_key and not the id.

I had the same error when I was trying to get entities with ObjectManager in ManyToMany realtionship:

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

My workaround was to write find method in Repository with DQL:

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? The conf.papers variable may be null and you would need to check if it's null first before counting. {% if conf.papers is empty %} ... {% endif %}

I would guess it could be caused by the plural case: paper s

Try this:

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

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