关于doctrine m:n关系的未定义索引

时间:2011-09-12 12:40:31

标签: php symfony1 doctrine

我有一个'部门'和'newsItem',它们与m:n有关。每当我尝试枚举某个部门的newsItems,从而触发从db中检索时,我就会收到此错误:

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
    );
}
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

3 个答案:

答案 0 :(得分:30)

我遇到了同样的麻烦,StackOverflow上的答案虽然正确但不够深入。我找到了其中一个Doctrine jira issues

如果你跑

doctrine:schema:validate
然后它会告诉你问题是什么。

答案 1 :(得分:20)

如果我是对的......

类Department中的mappedBy应该是NewsItem类的属性,在你的case部门中,而不是newsItems。

答案 2 :(得分:0)

你可能在函数符号中遗漏了一些东西..看看这个:

http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#many-to-many-bidirectional

看来你错过了@JoinTable表示法 - 也许试着颠倒Owning和Inverse方面?