[英]Symfony2 and Doctrine - Undefined index
我有一個Symfony 2.4.x項目。
在那里,我有兩個映射在一起的實體:會議和論文。
每個會議都有論文和特定會議,我想獲得論文數量。
為此,在我的會議實體中,我有:
/**
* @ORM\OneToMany(targetEntity="Paper", mappedBy="conference")
*/
protected $papers;
在實體論文中我有:
/**
* @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:這是我通過嘗試解決問題找到的一些消息。 另有一個classe參與其中:提交。
這是代碼: http : //pastebin.com/bkdRtjdq
在Submission類中,我有主鍵是hash_key而不是id。
當我試圖在ManyToMany
使用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();
}
也許對某人有用。
是否存在論文為0的情況? conf.papers變量可能為null,您需要在計數之前先檢查它是否為null。 {%if conf.papers為空%} ... {%endif%}
我想這可能由多種情況造成的:紙S
嘗試這個:
/**
* @ORM\OneToMany(targetEntity="Paper", mappedBy="conference")
*/
protected $paper;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.