我在 Doctrine 2(使用 Symfony2)中映射实体时遇到问题。 Symfony 说错误出现在 Perfil 实体中,但 Doctrine 部分分析器显示错误出现在 Funcion 实体中。
这是错误(在浏览器上):
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.
但是,在分析器中:
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'.
这是关系:
Perfil 实体基本上是:
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...
}
Function 实体基本上是:
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...
}
有什么想法吗?谢谢!
最佳答案
我认为默认情况下 Doctrine 依赖于实体中的所有主键都由名为 $id
的属性表示的约定。两种解决方案:
- 将主键从
$idperfil
和$idfuncion
重命名为$id
(我推荐); 在
$perfiles
属性上显式显示Funcion
对象的连接条件:* @ORM\JoinTable(name="perfil_function"), * joinColumns={@ORM\JoinColumn(name="funcion", referencedColumnName="idfuncion")}, * inverseJoinColumns={@ORM\JoinColumn(name="perfil", referencedColumnName="idperfil")} * )
关于symfony - 学说未找到实体 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917104/