php - name - Índice indefinido sobre la doctrina m: n relación
undefined index action in (3)
Tengo un ''departamento'' y ''newsItem'', que están relacionados m: n. Cada vez que intento enumerar los artículos de noticias de un departamento, desencadenando la recuperación desde el archivo db, aparece este 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) ) )
Lo que es especialmente extraño es que hay otra relación en el departamento, con otra entidad m: n, que simplemente funciona, y no hay diferencia en cómo se establece el mapeo. Lo comprobé 10 veces al menos.
Las clases y la stacktrace completa:
/**
* @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
Es probable que te falte algo en las anotaciones de tu función. Echa un vistazo a esto:
Parece que te falta la notación @JoinTable. ¿Quizás intentes invertir la posesión y el lado inverso?
Estoy teniendo el mismo problema, y las respuestas en son correctas, pero no lo suficientemente profundas. Encontré en uno de los problemas de jira de Doctrine :
Si tu corres
doctrine:schema:validate
entonces te dirá cuál es el problema.
Si estoy en lo correcto ...
Su mappedBy en la clase Department debe ser propiedad de la clase NewsItem, en los departamentos de su caso en lugar de newsItems.