Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Sonata Admin存储MongoDB\ReferenceOne?_Mongodb_Symfony_Doctrine_Sonata Admin - Fatal编程技术网

如何使用Sonata Admin存储MongoDB\ReferenceOne?

如何使用Sonata Admin存储MongoDB\ReferenceOne?,mongodb,symfony,doctrine,sonata-admin,Mongodb,Symfony,Doctrine,Sonata Admin,我在Sonata管理和在MongoDB文档中存储引用时遇到问题。我正确设置了Sonata Admin(无需参考,效果良好)。 但是,如果我试图为cinema文档存储用户引用,则会出现以下错误 错误 Symfony\Component\Validator\ConstraintViolation Object(Symfony\Component\Form\Form).children[user_id] = 5511710289d39769378b4567 Caused by: Symfony\C

我在Sonata管理和在MongoDB文档中存储引用时遇到问题。我正确设置了Sonata Admin(无需参考,效果良好)。 但是,如果我试图为cinema文档存储用户引用,则会出现以下错误

错误

Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[user_id] = 5511710289d39769378b4567

Caused by:

Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "user_id": The choice "5511710289d39769378b4567" does not exist or is not unique

Caused by:

Symfony\Component\Form\Exception\TransformationFailedException
The choice "5511710289d39769378b4567" does not exist or is not unique
Cinema.php

namespace Cinemato\AppBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Cinemato\UserBundle\Document\User as User;

/**
 * @MongoDB\Document
 */
class Cinema
    {

    /**
     *
     * @var integer 
     * @MongoDB\Id
     */
    private $id;

    /**
     *
     * @var string 
     * @MongoDB\String
     */
    private $title;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Cinemato\UserBundle\Document\User")
     */
    private $user_id;

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

    /**
     * Set userId
     *
     * @param Cinemato\AppBundle\Document\User $userId
     * @return self
     */
    public function setUserId(\Cinemato\AppBundle\Document\User $userId)
    {
        $this->user_id = $userId;
        return $this;
    }

    /**
     * Get userId
     *
     * @return Cinemato\AppBundle\Document\User $userId
     */
    public function getUserId()
    {
        return $this->user_id;
    }
}

要解决此问题,必须编辑
cinemadmin.php
,必须在
configureFormFields
中添加
sonata\u type\u model\u列表

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('title')
        ->add('user_id', 'sonata_type_model_list')
    ;
}