PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 31, 2022

[FIXED] How to join two entity methods as one

 January 31, 2022     methods, php, symfony     No comments   

Issue

Based on a private messaging app, my user entity currently have two methods to retrieve messages :

One to get the messages sent by the user

/**
 * @ORM\OneToMany(targetEntity="App\Entity\MessagePrive", mappedBy="emetteur")
 */
private $messagesPrivesEmis;

/**
 * @return Collection|MessagePrive[]
 */
public function getMessagesPrivesEmis(): Collection {
    return $this->messagesPrivesEmis;
}

and another one to get the messages received from other users

/**
 * @ORM\OneToMany(targetEntity="App\Entity\MessagePrive", mappedBy="recepteur")
 */
private $messagesPrivesRecus;

/**
 * @return Collection|MessagePrive[]
 */
public function getMessagesPrivesRecus(): Collection {
    return $this->messagesPrivesRecus;
}

The first method get the messages where emetteur is equal to user id, while the second get the messages where recepteur is equal to user id. Both are Symfony default methods

Is it possible to "merge" those two methods so it get all messages sent and received by the user in one single query?
Or should I resort to custom DQL?


Solution

public function getMerged(): Collection {
   return new ArrayCollection(
       array_merge(this->messagesPrivesEmis->toArray(), $this->messagesPrivesRecus->toArray())
   );
}


Answered By - craigh
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing