Reputation: 394
So I have the next documents:
/*
* @ODM\Document(collection="role", repositoryClass="AppBundle\Document\Repository\RoleRepository")
*/
class Role implements RoleInterface
{
/**
* @var integer $id
*
* @ODM\Id(strategy="INCREMENT")
*/
private $id;
/**
* @var string $name
*
* @ODM\Field(type="string")
*/
private $name;
/**
* @var ArrayCollection $users
*
* @ODM\ReferenceMany(targetDocument="User", mappedBy="roles", cascade={"all"})
*
* @JMS\Accessor(getter="getUsersToJson")
* @JMS\Expose
* @JMS\Type("array<integer>")
*/
private $users;
and
/**
* @ODM\Document(collection="user", repositoryClass="AppBundle\Document\Repository\UserRepository")
*/
class User implements AdvancedUserInterface
{
/**
* @var integer $id
*
* @ODM\Id(strategy="INCREMENT")
* @ODM\Index(unique=true)
*
* @JMS\Expose
* @JMS\Type("integer")
*/
private $id;
/**
* @var ArrayCollection $roles
*
* @ODM\ReferenceMany(targetDocument="Role", inversedBy="users", cascade={"persist", "refresh"})
*
* @JMS\Expose
* @JMS\Type("ArrayCollection<AppBundle\Document\Role>")
*/
private $roles;
So when I do the patch request to update role I want to set update the list of users which role has. I it completely occurs, but when I go to the database I do not see the role in user which was added in role's user list or I do see the role which I removed from role's user list. So is this a normal behaviour and I have to delete these references by myself OR I do something wrong?
UPDATE:
Always need to write the question to understand what do I really want...
For editing I use symfony forms.
The main question is: can we have reference with something like both inversedBy relation? I want to have opportunity to edit user and edit role and they remove or add ids from each other, which they (do not) use.
Upvotes: 0
Views: 573
Reputation: 327
Currently no, you cannot. One model must own the relationship on one side or the other.
If you have the User
model own the relationship to the Role
you can set a role(s) to the User while editing it. The users
property on the Role model can be an inverse reference to display what users have the Role assigned, but you would have to modify the model with the owning reference to add more users to the role.
Upvotes: 1