Answer the question
In order to leave comments, you need to log in
Symfony2 DoctrineMongoDBBundle one-to-many bidirectional links?
In the Doctrine MongoDB Object Document Mapper
presentation (slide 47), the User object is assigned an Organization, and this organization automatically has a link to it in the Users array, which can be seen on slide 49.
The documentation says that I should use the inversedBy and mappedBy options.
I have the same dependency (User and Groups) and I want to repeat this, however, I can't get automatic updating of links for dependent objects, then the code is:
$Group = new \MyVendor\MongoBundle\Document\Group();<br>
$User = new \MyVendor\MongoBundle\Document\User();<br>
$User->setGroup($Group);<br>
/** @var \Doctrine\ODM\MongoDB\DocumentManager $dm */<br>
$dm = $this->get('doctrine_mongodb')->getManager();<br>
$dm->persist($Group);<br>
$dm->persist($User);<br>
$dm->flush();<br>
{
"_id": ObjectId("5043e24acdc2929a0500000d"),
}
{
"_id": ObjectId("5043e24acdc2929a0500000c"),
"group": {
"$ref": "Group",
"$id": ObjectId("5043e24acdc2929a0500000d"),
"$db": "my_db"
}
}
<?php<br>
namespace MyVendor\MongoBundle\Document;<br><br>
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;<br><br>
/**<br>
* @MongoDB\Document(repositoryClass="MyVendor\MongoBundle\Repository\UserRepository")<br>
*/<br>
class User<br>
{<br>
/**<br>
* @MongoDB\Id<br>
*/<br>
private $id;<br><br>
/**<br>
* @var<br>
* @MongoDB\ReferenceOne(targetDocument="Group", inversedBy="users")<br>
*/<br>
private $group;<br><br>
/**<br>
* Set group<br>
*<br>
* @param MyVendor\MongoBundle\Document\Group $group<br>
* @return User<br>
*/<br>
public function setGroup(\MyVendor\MongoBundle\Document\Group $group)<br>
{<br>
$this->group = $group;<br>
return $this;<br>
}<br>
}<br>
<?php<br>
namespace MyVendor\MongoBundle\Document;<br><br>
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;<br><br>
/**<br>
* @MongoDB\Document<br>
*/<br>
class Group<br>
{<br><br>
/**<br>
* @MongoDB\Id<br>
*/<br>
private $id;<br><br>
/**<br>
* @MongoDB\ReferenceMany(targetDocument="User", mappedBy="group")<br>
* @var User[]<br>
*/<br>
private $users;<br><br>
public function __construct()<br>
{<br>
$this->users = new \Doctrine\Common\Collections\ArrayCollection();<br>
}<br><br>
/**<br>
* Add users<br>
*<br>
* @param MyVendor\MongoBundle\Document\User $users<br>
*/<br>
public function addUsers(\MyVendor\MongoBundle\Document\User $users)<br>
{<br>
$this->users[] = $users;<br>
}<br>
}<br>
Answer the question
In order to leave comments, you need to log in
Obviously, this is not a task for the datamapper, but for the logic of your entities.
<?php
namespace MyVendor\MongoBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document(repositoryClass="MyVendor\MongoBundle\Repository\UserRepository")
*/
class User
{
/**
* @MongoDB\Id
*/
private $id;
/**
* @var
* @MongoDB\ReferenceOne(targetDocument="Group", inversedBy="users")
*/
private $group;
/**
* Set group
*
* @param MyVendor\MongoBundle\Document\Group $group
* @return User
*/
public function setGroup(\MyVendor\MongoBundle\Document\Group $group)
{
$this->group = $group;
$group->addUsers($this);
return $this;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question