K
K
karser2012-09-03 02:54:21
symfony
karser, 2012-09-03 02:54:21

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>

Result in MongoDB:
Group
{
   &quot;_id&quot;: ObjectId(&quot;5043e24acdc2929a0500000d&quot;),
}

user
{
   &quot;_id&quot;: ObjectId(&quot;5043e24acdc2929a0500000c&quot;),
   &quot;group&quot;: {
     &quot;$ref&quot;: &quot;Group&quot;,
     &quot;$id&quot;: ObjectId(&quot;5043e24acdc2929a0500000d&quot;),
     &quot;$db&quot;: &quot;my_db&quot; 
  }
}

src/MyVendor/MongoBundle/Document/User.php
<?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>

src/MyVendor/MongoBundle/Document/Group.php
<?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>

Cross question on stackoverflow

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nikita2206, 2012-09-03
@nikita2206

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 question

Ask a Question

731 491 924 answers to any question