Reputation: 2029
i have a problem mapping entities in Doctrine 2 (with Symfony2). Symfony says that error is in Perfil entity, but the Doctrine section profiler says that the error is en Funcion entity.
This is the error (on the browser):
The column id must be mapped to a field in class AppsManantiales\CommonBundle\Entity\Perfil since it is referenced by a join column of another class.
But, in the profiler:
AppsManantiales\CommonBundle\Entity\Funcion: The referenced column name 'id' has to be a primary key column on the target entity class 'AppsManantiales\CommonBundle\Entity\Funcion'. The referenced column name 'id' has to be a primary key column on the target entity class 'AppsManantiales\CommonBundle\Entity\Perfil'.
This is the relation er:
The Perfil entity, basically is:
class Perfil implements RoleInterface{
/**
* @ORM\Id
* @ORM\Column(type="integer", length=10)
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $idperfil;
/**
* @ORM\ManyToMany(targetEntity="Funcion", mappedBy="perfiles")
* @ORM\JoinTable(name="perfil_funcion",
* joinColumns={@ORM\JoinColumn(name="perfil", referencedColumnName="idperfil")},
* inverseJoinColumns={@ORM\JoinColumn(name="funcion", referencedColumnName="idfuncion")}
* )
*/
protected $funciones;
// More code...
}
The Funcion entity basically is:
class Funcion {
/**
* @ORM\Id
* @ORM\Column(type="integer", length=11)
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $idfuncion;
/**
* @ORM\ManyToMany(targetEntity="Perfil", inversedBy="funciones")
*/
protected $perfiles;
// More code...
}
Any ideas ? Thanks !
Upvotes: 2
Views: 3406
Reputation: 183
I think by default Doctrine relies on the convention that all primary keys in entities are represented by an attribute called $id
. Two solutions :
$idperfil
and $idfuncion
to $id
(which I recommend);Explicit the join criterion on your Funcion
object on the $perfiles
property:
* @ORM\JoinTable(name="perfil_function"),
* joinColumns={@ORM\JoinColumn(name="funcion", referencedColumnName="idfuncion")},
* inverseJoinColumns={@ORM\JoinColumn(name="perfil", referencedColumnName="idperfil")}
* )
Upvotes: 4