Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Paper: http://pastebin.com/Fv7m70VN
  • Conference: http://pastebin.com/dbCsH7Nh

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.

like image 255
Milos Cuculovic Avatar asked Jan 13 '14 14:01

Milos Cuculovic


2 Answers

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.

like image 80
Aistis Avatar answered Nov 07 '22 04:11

Aistis


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

Try this:

/**
 * @ORM\OneToMany(targetEntity="Paper", mappedBy="conference")
 */
protected $paper;
like image 2
zavalit Avatar answered Nov 07 '22 06:11

zavalit