基于私人消息應用程序,我的用戶實體目前有兩種檢索消息的方法:一、獲取用戶發送的消息/** * @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 關注
- 175 瀏覽
添加回答
舉報
0/150
提交
取消