dpmpa26468
2016-01-26 10:30
浏览 35
已采纳

Symfony - 映射彼此不一致

I have 2 Entities, User and Follower.

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User extends BaseUser
{
    /**
     * @ORM\OneToMany(targetEntity="Follower", mappedBy="user")
     */
    protected $followers;

    /**
     * @ORM\OneToMany(targetEntity="Follower", mappedBy="follower")
     */
    protected $followings;
}


/**
 * @ORM\Entity
 * @ORM\Table(name="follows")
 */
class Follower
{        
    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="followers")
     */
    protected $user;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="followers")
     */
    protected $follower;
}

User have followers ($followers) and followings ($followings).

I'm not sure why, but my dev profiler says:

The mappings AppBundle\Entity\User#followings and AppBundle\Entity\Follower#follower are inconsistent with each other.

The mappings AppBundle\Entity\Follower#follower and AppBundle\Entity\User#followers are inconsistent with each other.

Why they are incosistent and it should be done?

图片转代码服务由CSDN问答提供 功能建议

我有2个实体,用户和追随者。

  /  ** 
 * @ORM \ Entity 
 * @ORM \ Table(name =“users”)
 * / 
class用户扩展BaseUser 
 {
 / ** 
 * @ORM \ OneToMany(targetEntity =  “Follower”,mappedBy =“user”)
 * / 
 protected $ followers; 
 
 / ** 
 * @ORM \ OneToMany(targetEntity =“Follower”,mappedBy =“follower”)
 *  / 
 protected $ followings; 
} 
 
 
 / ** 
 * @ORM \ Entity 
 * @ORM \ Table(name =“follow”)
 * / 
class Follower 
 {  
 / ** 
 * @ORM \ ManyToOne(targetEntity =“User”,inversedBy =“followers”)
 * / 
 protected $ user; 
 
 / ** 
 * @ORM \ ManyToOne(  targetEntity =“User”,inversedBy =“followers”)
 * / 
 protected $ follower; 
} 
   
 
 

用户有关注者($粉丝)和 以下($ followings)。

我不知道为什么,但我的开发人员说:

映射AppBundle \ 实体\用户#followings和 AppBundle \ Entity \ Follower#follower不一致 相互依赖。

映射AppBundle \ Entity \ Follower#follower和 AppBundle \ Entity \ User#followers彼此不一致。

为什么它们不存在且应该完成?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doushan9415 2016-01-26 11:07
    已采纳

    In the Follower Entity, replace this:

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="followers")
     */
    protected $follower;
    

    with:

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="followings")
     */
    protected $follower;
    

    You can use the command doctrine:schema:validate that checks the current mapping for valid forward and reverse mappings.

    php app/console doctrine:schema:validate

    Hope this help

    点赞 打赏 评论
  • douhao6271 2016-01-26 11:09

    You should replace followers by followings in :

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="followers")
     */
    protected $follower;
    

    But i think it's better to use ManyToMany associations on User Entity. You can try something like this :

    /**
     * @ORM\Entity
     * @ORM\Table(name="users")
    */
    class User extends BaseUser
    {
      /**
       * @ORM\ManyToMany(targetEntity="User", mappedBy="followings")
      */
      private $followers;
    
      /**
       * @ORM\ManyToMany(targetEntity="User", inversedBy="followers")
       * @ORM\JoinTable(name="follows",
       *      joinColumns={@ORM\JoinColumn(name="following_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="follower_id", referencedColumnName="id")}
       *      )
      */
      private $followings;
    
    点赞 打赏 评论

相关推荐 更多相似问题