U
U
uzh132017-03-20 13:36:51
symfony
uzh13, 2017-03-20 13:36:51

How to filter objects by ManyToMany relationship in Doctrine?

There is an object that has an editors link, which defines users who have rights to edit this object.
Communication is defined in the standard way:

// app/src/MyBundle/Entity/Object.php
/**
* @ORM\ManyToMany(targetEntity="User")
* @ORM\JoinTable(name="object_redactors)
*/
private $redactor

In the controller, you need to find all the objects that have a specific user specified in the editors.
How to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GTRxShock, 2017-03-20
@uzh13

I think the point is clear:
Object.php

/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="objects")
* @ORM\JoinTable(name="objects_redactors")
*/
protected $redactors;

user.php
/**
* @ORM\ManyToMany(targetEntity="Object", mappedBy="redactors")
*/
protected $objects;

Next, call getObjects() on that same specific user

S
shagguboy, 2017-03-20
@shagguboy

findByUser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question