Symfony2 DoctrineMongoDB对象不保存在数据库中

时间:2015-12-28 18:29:21

标签: mongodb symfony oop object save

我实际上使用Symfony 2.8.0和DoctrineMongoDB ODM。我有一个FOSUser表,它引用了一个Client表,如下所示:

user.php的

<?php

namespace ACE\UserBundle\Document;

use Doctrine\Common\Collections\ArrayCollection;
use FOS\UserBundle\Document\User as BaseUser;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 */
class User extends BaseUser
{
    /**
     * @MongoDB\Id(strategy="auto")
     */
    protected $id;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Client", inversedBy="users")
     */
    protected $client;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

    /**
     * Set client
     *
     * @param ACE\UserBundle\Document\Client $client
     * @return self
     */
    public function setClient(\ACE\UserBundle\Document\Client $client)
    {
        $this->client = $client;
        return $this;
    }

    /**
     * Get client
     *
     * @return ACE\UserBundle\Document\Client $client
     */
    public function getClient()
    {
        return $this->client;
    }
}

Client.php

<?php

namespace ACE\UserBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 */
class Client
{
    /**
     * @MongoDB\Id(strategy="auto")
     */
    protected $id;

    /**
     * @MongoDB\ReferenceMany(targetDocument="User", cascade={"all"}, mappedBy="client")
     */
    private $users;

    /**
     * @MongoDB\String
     */
    private $ref;

    public function __construct()
    {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return id $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add user
     *
     * @param \ACE\UserBundle\Document\User $user
     */
    public function addUser(\ACE\UserBundle\Document\User $user)
    {
        $this->users[] = $user;
    }

    /**
     * Remove user
     *
     * @param \ACE\UserBundle\Document\User $user
     */
    public function removeUser(\ACE\UserBundle\Document\User $user)
    {
        $this->users->removeElement($user);
    }

    /**
     * Get users
     *
     * @return \Doctrine\Common\Collections\Collection $users
     */
    public function getUsers()
    {
        return $this->users;
    }

    /**
     * Set ref
     *
     * @param string $ref
     * @return self
     */
    public function setRef($ref)
    {
        $this->ref = $ref;
        return $this;
    }

    /**
     * Get ref
     *
     * @return string $ref
     */
    public function getRef()
    {
        return $this->ref;
    }
}

但是当我想将用户持久存入Client表时,用户不会保存在数据库中......

$user = $this->container->get('security.context')->getToken()->getUser();
$client = new Client();
$client->addUser($user);
$client->setRef("1234");

$dm = $this->get('doctrine_mongodb')->getManager();
$dm->persist($client);
$dm->flush();

有什么想法吗?我在网上搜索,但每个解决方案都不起作用......

感谢您的回答!

0 个答案:

没有答案