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

Symfony2 DoctrineMongoDB对象未保存在数据库中,mongodb,symfony,oop,object,save,Mongodb,Symfony,Oop,Object,Save,实际上,我使用的是Symfony 2.8.0和DoctrineMongoDB ODM。我有一个FOSUser表,它引用了客户机表,如下所示: User.php <?php namespace ACE\UserBundle\Document; use Doctrine\Common\Collections\ArrayCollection; use FOS\UserBundle\Document\User as BaseUser; use Doctrine\ODM\MongoDB\Mapp

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

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;
    }
}
有什么想法吗?我在网上搜索过,但每一个解决方案都不起作用


谢谢你的回答

您是否在日志中检查查询是否确实发送到了您的MongoDB?您如何确保数据不会持久化?因为id和ref存储在数据库中,而不是用户。。。symfony分析器发送给我:db.Client.insert({“000000003f6049f100007fd3a310400b”:{“_id”:ObjectId(“56819239336f510916958bc4”),“ref”:“1234”});您在用户对象中使用了反向侧,这意味着用户文档中只能引用客户端文档,反之亦然!阅读10次或更多次请记住,反向侧,指定mappedBy的侧是不可变的,对引用状态的任何更改都不会被持久化。因此,我应该删除mappedBy关键字?将
mappedBy
更改为
inversedBy
,反之亦然。
<?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;
    }
}
$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();