如何使用Sonata Admin存储MongoDB\ReferenceOne?
我在Sonata管理和在MongoDB文档中存储引用时遇到问题。我正确设置了Sonata Admin(无需参考,效果良好)。 但是,如果我试图为cinema文档存储用户引用,则会出现以下错误 错误如何使用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
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')
;
}