这是我的用户实体:(是的,自我参数的ManyToMany)
class User extends BaseUser{
/**
*@ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="myFriends")
*/
private $friendsWithMe;
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="friendsWithMe")
* @ORM\JoinTable(name="friends",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="friend_user_id", referencedColumnName="id")}
* )
*/
private $myFriends;
/**
* @ORM\OneToMany(targetEntity="Activity", mappedBy="user")
*/
private $activities;
这是我的活动实体:
class Activity
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="date")
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="activities")
*/
private $user;
这是我想要执行的查询:
SELECT activity.*
FROM activity
JOIN (SELECT friend_user_id, user_id FROM friends WHERE friends.user_id = 1 OR friends.friend_user_id = 1 ) f
ON activity.user_id IN (f.user_id,f.friend_user_id)
问题:
我正在尝试在doctrine2中创建此查询,但我无法获得如何加入表“朋友”的方式 - 此表没有相应的实体,它基于Users.id-to-Users。 id关系。