php 学说 m:n 关系的未定义索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7388017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined index on doctrine m:n relation
提问by Jan
I have a 'department' and 'newsItem', which are related m:n. Whenever I try to enumerate over a department's newsItems, thus triggering retrieval from the db, I get this error:
我有一个“部门”和“新闻项目”,它们与 m:n 相关。每当我尝试枚举部门的 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) ) )
What is especially weird is, that there is another relation in department, to another m:n entity, that just works, and there is no difference in how the mapping is set up, I checked 10 times at least.
特别奇怪的是,部门中有另一种关系,与另一个 m:n 实体,这只是有效,并且映射的设置方式没有区别,我至少检查了 10 次。
The classes and the full stacktrace:
类和完整的堆栈跟踪:
/**
* @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
);
}
at ErrorHandler ->handle ('8', 'Undefined index: 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))) in /.../vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php at line 807 at 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)) in /.../vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php at line 778 at 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)) in /.../vendor/doctrine/lib/Doctrine/ORM/UnitOfWork.php at line 2088 at UnitOfWork ->loadCollection (object(PersistentCollection)) in /.../vendor/doctrine/lib/Doctrine/ORM/PersistentCollection.php at line 207 at PersistentCollection ->initialize () in /.../vendor/doctrine/lib/Doctrine/ORM/PersistentCollection.php at line 474 at PersistentCollection ->count () in at line at count (object(PersistentCollection)) in /.../src/UfscarDfmc/OrgBundle/Controller/DepartmentController.php at line 53 at DepartmentController ->showAction ('graduacao') in at line
回答by Bae
I'm having the same trouble, and the answers on StackOverflow while correct dont go deep enough. I found in one of the Doctrine jira issues:
我遇到了同样的问题,并且 StackOverflow 上的答案虽然正确但不够深入。我在Doctrine jira 问题之一中发现:
If you run
如果你跑
doctrine:schema:validate
then it will tell you what the problem is.
然后它会告诉你问题是什么。
回答by pderaaij
If I am correct...
如果我是对的...
Your mappedBy in the class Department should be the property of the NewsItem class, in your case departments in stead of newsItems.
您在类 Department 中的 mappingBy 应该是 NewsItem 类的属性,在您的案例中是部门而不是 newsItems。
回答by geezmo
You are probably missing something in your function notations.. have a look at this:
你可能在你的函数符号中遗漏了一些东西..看看这个:
It seems you are missing @JoinTable notation - maybe try to invert Owning and Inverse side?
似乎您缺少 @JoinTable 符号 - 也许尝试反转拥有和反向?