关于学说 m:n 关系的未定义索引
Posted
技术标签:
【中文标题】关于学说 m:n 关系的未定义索引【英文标题】:Undefined index on doctrine m:n relation 【发布时间】:2011-11-15 07:48:15 【问题描述】:我有一个“部门”和“新闻项目”,它们是相关的 m:n。每当我尝试枚举部门的 newsItems,从而触发从数据库中检索时,我都会收到此错误:
在 ErrorHandler -> 句柄 ( '8', '未定义索引:newsItems', '/.../ufscar_symfony/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php', '807', 大批( '关联' => 数组( 'fieldName' => 'newsItems', 'joinTable' => 数组(), 'targetEntity' => 'UfscarDfmc\OrgBundle\Entity\NewsItem', 'mappedBy' => 'newsItems', 'inversedBy' => 空, '级联' => 数组(), '获取' => '2', '类型' => '8', 'isOwningSide' => 假的, 'sourceEntity' => 'UfscarDfmc\OrgBundle\Entity\Department', 'isCascadeRemove' => 假, 'isCascadePersist' => 假, 'isCascadeRefresh' => 假, 'isCascadeMerge' => 假, 'isCascadeDetach' => 假 ), 'sourceEntity' => 对象(部门), '偏移' => 空, '限制' => 空, '标准' => 数组(), 'sourceClass' => 对象(类元数据) ) )特别奇怪的是,department 中有另一个关系,与另一个 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
);
在 ErrorHandler ->handle ('8', '未定义索引: newsItems', '/.../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)))
在 /.../vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php 第 807 行
在 BasicEntityPersister ->getManyToManyStatement (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), object(Department))
在 /.../vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php 第 778 行
在 BasicEntityPersister ->loadManyToManyCollection (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), object(Department), object(PersistentCollection ))
在 /.../vendor/doctrine/lib/Doctrine/ORM/UnitOfWork.php 第 2088 行
在 UnitOfWork ->loadCollection (object(PersistentCollection))
在 /.../vendor/doctrine/lib/Doctrine/ORM/PersistentCollection.php 第 207 行
在 PersistentCollection -> 初始化 ()
在 /.../vendor/doctrine/lib/Doctrine/ORM/PersistentCollection.php 第 474 行
在 PersistentCollection ->count ()
在线上
在计数(对象(PersistentCollection))
在 /.../src/UfscarDfmc/OrgBundle/Controller/DepartmentController.php 第 53 行
在 DepartmentController -> showAction ('graduacao')
在线上
【问题讨论】:
【参考方案1】:我遇到了同样的问题,*** 上的答案虽然正确,但还不够深入。我在Doctrine jira issues之一中找到了:
如果你运行
doctrine:schema:validate
然后它会告诉你问题是什么。
【讨论】:
我非常感谢您的这个提示。你刚刚拯救了我的一天 @johnkork 欢迎您。你不知道我搜索了多少次答案,发现我多年前就自己回答了!我很高兴其他人也从中受益。 现在orm:schema-tool
。例如。 ./vendor/bin/doctrine orm:schema-tool
【参考方案2】:
如果我是对的...
您在 Department 类中的 mappedBy 应该是 NewsItem 类的属性,在您的案例中是部门而不是 newsItems。
【讨论】:
100% 正确!有时似乎 10 次是不够的 :) 非常感谢! 我曾多次犯过同样的错误。很容易忘记您需要相关类的属性:)【参考方案3】:你可能在函数符号中遗漏了一些东西。看看这个:
http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#many-to-many-bidirectional
您似乎缺少 @JoinTable 表示法 - 也许尝试反转 Owning 和 Inverse 方?
【讨论】:
注意:pderaaij 的回答似乎比我的聪明得多。 :-)以上是关于关于学说 m:n 关系的未定义索引的主要内容,如果未能解决你的问题,请参考以下文章