简体   繁体   中英

How to store MongoDB\ReferenceOne with Sonata Admin?

I have a problem Sonata Admin and storing a reference in a MongoDB document. I set up Sonata Admin correct (without reference it works well). But if i try to store a user-reference for the cinema-document, i get the following error.

Error

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;
    }
}

To solve the problem the CinemaAdmin.php must be edited - sonata_type_model_list must be added in configureFormFields .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM