symfony - 学说未找到实体 ID

标签 symfony doctrine-orm

我在 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'.

这是关系:

enter image description here

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/

相关文章:

php - Symfony 如何验证以 base64 编码的传入图像?

php - Silex 安全提供商

php - 我如何在学说查询生成器中放置条件

php - Symfony - 多对多关系刷新时的内存问题

php - Symfony3 错误命名空间不包含任何映射实体

php - 具有国际扩展问题的 Symfony2。我似乎无法启用扩展

javascript - 单页应用程序 - 前端独立于后端?

symfony - Doctrine PostLoad 生命周期回调未在 fetchJoin 中执行

php - 如何编写一个依赖 Doctrine 并提供实体的 symfony 包?

php - 将行分组为一行的学说和 SUM 数量不起作用