我有一个'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行引发错误。
orm:schema-tool
。例如:./vendor/bin/doctrine orm:schema-tool
。 - Rainer Rillke