Answer the question
In order to leave comments, you need to log in
What is the correct way to get a piece of data from a relation in Symfony?
There are 3 tables.
user (id, username, email, etc.)
group (id, name)
membership (id, user_id, group_id, role)
The user and group have ManyToMany annotations.
I need to implement the getOwner() method on the group, which will return the user with the appropriate role.
What is the best way to do this? Take all users through annotations and filter on the php side, make a separate request without annotations, or can annotations be done somehow?
I'm not banned on Google, I just don't even know how to google it.
Answer the question
In order to leave comments, you need to log in
For these cases, you can use the matching method of ArrayCollection. We pass the prepared Criteria object there, with the help of which we set up the necessary filtering. When we pull the getOwner method, a query will be executed that will get only the data we need, without getting all the related records.
use Doctrine\Common\Collections\Criteria;
/**
* @ORM\Entity
*/
class Group {
// ...
public function getOwner() {
$criteria = Criteria::create()->where(фильтруем по роли или что-то ещё);
return $this->getUsers()->matching($criteria);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question