I have a problem with my many to many relations
I have a User class that can be on multiple Teams and a class Team that can have multiple Users
class User
/**
* @var Collection
* @ODM\ReferenceMany(
* targetDocument="App\Model\Document\Team",
* mappedBy="members",
* strategy="setArray"
* )
*/
protected $teams;
class Team
/**
* @var Collection|null
* @ODM\ReferenceMany(
* targetDocument="App\Model\Document\User",
* inversedBy="teams",
* strategy="setArray",
* sort={"username": "asc"},
* cascade={"persist"}
* )
*/
protected $members;
With these annotations, when I add members User to the team and I do a getMembers
on an instance of Team, it works.
BUT, when I have an instance of a User (that's a member of a Team) the getTeams
return nothing (empty PersistentCollection)
The reference is stored on the Teams Document, as an array of references
"members" : [
{
"$ref" : "User",
"$id" : ObjectId("XXX"),
"$db" : "readDat"
}
],
I don't understand, with the annotations as they are now, I thought Doctrine would do something like this db.getCollection('Team').find({"members.$id":ObjectId("XXX")})
to have the teams but it seems not.
On the other hand, I tried to inverse the mappedBy
and inversedBy
, I can't even getMembers
(on a Team object) or getTeams
(on a User object) anymore, both are empty Collection.
How can I have a good Many to Many (without duplicate the references in both Document) ?