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

2022-08-30 15:02:50

我有一个“部门”和“newsItem”,它们是相关的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)
  )
)

特别奇怪的是,部门中存在另一种关系,与另一个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

答案 1

我遇到了同样的麻烦,StackOverflow上的答案虽然正确,但还不够深入。我在教义吉拉问题之一中发现:

如果您运行

doctrine:schema:validate

然后它会告诉你问题是什么。


答案 2

如果我是正确的...

您的 mapped 在类部门中应该是 NewsItem 类的属性,在您的情况下,是部门而不是 newsItems。


推荐