[英]Doctrine, Symfony3, Query entities from other entity array
I have a problem using Doctrine with my entities.我在实体中使用 Doctrine 时遇到问题。 See below for the question.请参阅下面的问题。
My entities:我的实体:
class User
{
// ...
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Friend", mappedBy="userB")
*/
private $friends;
// ...
/**
* Get friends
*
* @return ArrayCollection
*/
public function getFriends()
{
return $this->friends;
}
}
And和
class Friend
{
/**
* @var string
*
* @ORM\Column(name="relation", type="string", length=255)
*/
private $relation;
/**
* @var int
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="friendsWithMe")
*/
private $userA;
/**
* @var int
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="friends")
*/
private $userB;
}
And my UserController
has this:我的UserController
有这个:
/**
* Show the user's friendzone
*
* @Route("/friendzone", name="show_friends")
*/
public function showFriendzoneAction()
{
$friends = $this->getUser()->getFriends();
$users = ???
return $this->render('DevLeaguesBundle:User:show_reduced.html.twig', array(
'users' => $friends,
));
}
What I would like my controller to do is to define $users
as an array of Users fetched from $friends
.我希望我的控制器做的是将$users
定义$users
从$friends
获取的用户数组。 The solution might be very simple.解决方案可能非常简单。
for array you need to build dql-query in UserRepository and get result as array OR you can use relation getFriends and get array collection from your user entity.对于数组,您需要在 UserRepository 中构建 dql-query 并将结果作为数组获取,或者您可以使用关系 getFriends 并从您的用户实体获取数组集合。 But, i don't understand why you don't use manytomany relation, in which case friends will be User entities, not Friend entities.但是,我不明白你为什么不使用 manytomany 关系,在这种情况下,朋友将是用户实体,而不是朋友实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.