26
votes

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:

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.

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
3

3 Answers

26
votes

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.

52
votes

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:

If you run

doctrine:schema:validate

then it will tell you what the problem is.

1
votes

You are probably missing something in your function notations.. have a look at this:

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

It seems you are missing @JoinTable notation - maybe try to invert Owning and Inverse side?