Doctrine m:n关系的未定义索引。

30
我有一个'm:n'的 'department' 和 'newsItem' 相关联。每当我尝试枚举部门的newsItems,从而触发从数据库中检索时,我会收到以下错误:

at ErrorHandler ->handle (
  '8',
  'Undefined index: newsItems',
  '/.../ufscar_symfony/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php',
  '807',
  array(
    'assoc' => array(
      'fieldName' => 'newsItems',
      'joinTable' => array(),
      'targetEntity' => 'UfscarDfmc\OrgBundle\Entity\NewsItem',
      'mappedBy' => 'newsItems',
      'inversedBy' => null,
      'cascade' => array(),
      'fetch' => '2',
      'type' => '8',
      'isOwningSide' => false,
      'sourceEntity' => 'UfscarDfmc\OrgBundle\Entity\Department',
      'isCascadeRemove' => false,
      'isCascadePersist' => false,
      'isCascadeRefresh' => false,
      'isCascadeMerge' => false,
      'isCascadeDetach' => false
    ),
    'sourceEntity' => object(Department),
    'offset' => null,
    'limit' => null,
    'criteria' => array(),
    'sourceClass' => object(ClassMetadata)
  )
)

特别奇怪的是,这个部门还与另一个'm:n'实体有联系,那个关系却正常工作,而且映射设置没有任何区别,我至少检查了10次。

这些类和完整的堆栈跟踪:

     /**
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="...\OrgBundle\Entity\DepartmentRepository")
 */
class Department
{
        /**
     * Inverse Side
     *
     * @ManyToMany(targetEntity="NewsItem", mappedBy="newsItems")
     */
    private $newsItems;

    public function __construct()
    {
        $this->newsItems = new \Doctrine\Common\Collections\ArrayCollection();
    }
    /**
     * Get newsItems
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getNewsItems()
    {
        return $this->newsItems;
    }
}


class NewsItem
{
    /**
     * Owning Side
     *
     * @ManyToMany(targetEntity="Department", inversedBy="newsItems")
     * @JoinTable(name="newsItems_departments",
     *      joinColumns={@JoinColumn(name="newsItem_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="department_id", referencedColumnName="id")}
     *      )
     */
    private $departments;

    public function __construct(){
        $this->departments = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get departments
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getDepartments()
    {
        return $this->departments;
    }
}


public function showAction($slug)
{
    $em = $this->getDoctrine()->getEntityManager();
    $entity = $em->getRepository('UfscarDfmcOrgBundle:Department')->findOneBySlug($slug);

    return array(
        'entity' => $entity,
        'newsItems' => $entity->getNewsItems(), # enumerating over this gives the error
    );
}
在/.../vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php的807行,未定义索引“newsItems”,在DepartmentController.php的53行引发错误。
3个回答

67

我也遇到了同样的问题,尽管StackOverflow上的答案是正确的,但并不够深入。在Doctrine jira issues中,我发现:

如果你运行

doctrine:schema:validate

然后它会告诉你问题出在哪里。


5
非常感谢这个贴士,你刚刚救了我的一天。 - johnkork
@johnkork 不客气。你不知道我已经搜索了多少次答案,结果发现几年前我自己已经回答过了!我很高兴其他人也从中受益。 - Bae
现在流行使用 orm:schema-tool。例如:./vendor/bin/doctrine orm:schema-tool - Rainer Rillke

26

如果我没错的话...

你的Department类中的mappedBy属性应该是NewsItem类的属性,在你的情况下应该是departments而不是newsItems


3
完全正确!有时似乎10倍都不够 :) 非常感谢! - Jan
我犯了同样的错误几次。很容易忘记你需要相关类的属性 :) - pderaaij

1

请注意:pderaaij的答案比我的聪明得多。 - geezmo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接