如何使用 Sonata Admin 存储 MongoDB\ReferenceOne?
How to store MongoDB\ReferenceOne with Sonata Admin?
我在 Sonata Admin 和在 MongoDB 文档中存储参考时遇到问题。我正确设置了 Sonata Admin(没有参考它运作良好)。
但是,如果我尝试存储电影文档的用户参考,则会出现以下错误。
错误
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;
}
}
要解决此问题,必须编辑 CinemaAdmin.php
- sonata_type_model_list
必须添加到 configureFormFields
。
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->add('user_id', 'sonata_type_model_list')
;
}
我在 Sonata Admin 和在 MongoDB 文档中存储参考时遇到问题。我正确设置了 Sonata Admin(没有参考它运作良好)。 但是,如果我尝试存储电影文档的用户参考,则会出现以下错误。
错误
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;
}
}
要解决此问题,必须编辑 CinemaAdmin.php
- sonata_type_model_list
必须添加到 configureFormFields
。
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->add('user_id', 'sonata_type_model_list')
;
}