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

时间:2015-03-24 15:02:15

标签: mongodb symfony doctrine 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;
    }
}

1 个答案:

答案 0 :(得分:0)

要解决问题,必须修改CinemaAdmin.phpsonata_type_model_list必须添加configureFormFields

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