我有三个表 - 用户和朋友($owner,$friend(具有多对多关系和帖子 - 连接到用户一对多。朋友表是由@JoinTable创建的,我的问题是创建一个适当的查询,该查询将显示亲自登录的友好用户的帖子;
后存储库:
/**
* @param User $user
* @return array
*/
public function selectFriendsPost(User $user)
{
return $this->createQueryBuilder("post")
->select('user', 'friends', 'owners')
->leftJoin('user.friends', 'friends')
->leftJoin('user.owners', 'owners')
->where('post.owner = : friends')
->setParameter('userId', $user->getFriends())
->orderBy("post.createdAt", "DESC")
->getQuery()
->getResult();
}
用户实体:
/**
* @var Post[]ArrayCollection
*
* @ORMOneToMany(targetEntity="Post", mappedBy="owner")
* @ORMJoinColumn(name="owner_id", referencedColumnName="id")
*/
private $posts;
/**
* @var ArrayCollection
* @ORMManyToMany(targetEntity="User", inversedBy="owners")
* @ORMJoinTable(name="Friend",
* joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="friend_user_id", referencedColumnName="id")}
* )
*/
private $friends;
/**
* @var ArrayCollection
* @ORMManyToMany(targetEntity="User", mappedBy="friends")
*/
private $owners;
错误:
[Semantical Error] line 0, col 84 near '.friends friends': Error: Identification Variable user used in join path expression but was not defined before.
->leftJoin('BundleName:User','user','WITH','user.pkUser= post.refUser')
尝试以这种方式连接表。首先BundleName:EntityName
然后别名name
然后with
然后连接语句。