duanlang1531 2016-02-15 22:36
浏览 9
已采纳

来自另一个OneToOne协会的PFK的Doctrine 2 OneToOne

I have one question. I'm using Doctrine 2.0 and i want to know if is possible to made OneToOne association by using PFK created by another OneToOne assotiation as you can see on the picture below.

enter image description here

My code looks like this:

User class:

/**
 * @ORM\Entity()
 */
class User extends \Kdyby\Doctrine\Entities\IdentifiedEntity
{

    /**
     * @ORM\OneToOne(targetEntity="AllianceMember", mappedBy="user")
     * var AllianceMember
     */
    protected $allianceMembership;
}

AllianceMember class:

/**
 * @ORM\Entity()
 */
class AllianceMember extends \Kdyby\Doctrine\Entities\BaseEntity
{
    /**
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="User", inversedBy="allianceMembership")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
     * @var User
     */
    protected $user;

    /**
     * @ORM\OneToOne(targetEntity="AllianceRole", mappedBy="allianceMember")
     * var AllianceRole
     */
     protected $role;

AllianceRole class:

/**
 *
 * @ORM\Entity()
 */
class AllianceRole extends \Kdyby\Doctrine\Entities\BaseEntity
{
    /**
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="AllianceMember", inversedBy="role")
     * @ORM\JoinColumn(name="role_id", referencedColumnName="user_id", nullable=false)
     * @var AllianceMember
     */
    protected $allianceMember;
}

I'm getting this error, when I'm trying to get instance of User entity:

The column user_id must be mapped to a field in class App\Entity\AllianceMember since it is referenced by a join column of another class.

Is it even possible?

Thanks.

  • 写回答

1条回答 默认 最新

  • douti8321 2016-02-16 04:24
    关注

    Yes, using mapped entities for your @Id has been possible since doctrine 2.1 see Primary key and foreign key at the same time with doctrine 2

    I think you simply need to remove some of your JoinColumn statements. Since @user is already designated as your @Id, it isn't necessary to specify user_id as the JoinColumn:

    /**
     * @ORM\Entity()
     */
    class AllianceMember extends \Kdyby\Doctrine\Entities\BaseEntity
    {
        /**
         * @ORM\Id
         * @ORM\OneToOne(targetEntity="User", inversedBy="allianceMembership")
         * @var User
         */
        protected $user;
    
        /**
         * @ORM\OneToOne(targetEntity="AllianceRole", mappedBy="allianceMember")
         * var AllianceRole
         */
         protected $role;
    

    and

    /**
     *
     * @ORM\Entity()
     */
    class AllianceRole extends \Kdyby\Doctrine\Entities\BaseEntity
    {
        /**
         * @ORM\Id
         * @ORM\OneToOne(targetEntity="AllianceMember", inversedBy="role")
         * @var AllianceMember
         */
        protected $allianceMember;
    }
    

    The id of the AllianceMember is the user_id, and the id of the AllianceRole is the id of the AllianceMember, which is user_id.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效