基于私人消息应用程序,我的用户实体目前有两种检索消息的方法:一、获取用户发送的消息/** * @ORM\OneToMany(targetEntity="App\Entity\MessagePrive", mappedBy="emetteur") */private $messagesPrivesEmis;/** * @return Collection|MessagePrive[] */public function getMessagesPrivesEmis(): Collection { return $this->messagesPrivesEmis;}另一个用于获取从其他用户收到的消息/** * @ORM\OneToMany(targetEntity="App\Entity\MessagePrive", mappedBy="recepteur") */private $messagesPrivesRecus;/** * @return Collection|MessagePrive[] */public function getMessagesPrivesRecus(): Collection { return $this->messagesPrivesRecus;}第一种方法获取emetteur等于用户 id 的消息,而第二种方法获取等于用户 id 的消息recepteur。两者都是 Symfony 默认方法是否可以“合并”这两种方法,以便在一个查询中获取用户发送和接收的所有消息?还是我应该求助于自定义 DQL?
1 回答
慕森卡
TA贡献1806条经验 获得超8个赞
public function getMerged(): Collection {
return new ArrayCollection(
array_merge(this->messagesPrivesEmis->toArray(), $this->messagesPrivesRecus->toArray())
);
}
- 1 回答
- 0 关注
- 219 浏览
添加回答
举报
0/150
提交
取消
