简体   繁体   中英

Symfony2 & MongoDB : FormType of a document with references

I have a problem with one of my forms. I have a "Resource" document linked to 3 other documents :

  • Tags (ReferenceMany)
  • Category (ReferenceOne)
  • Data Repository (ReferenceOne)

Here is the definition of my document :

class Resource {

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

/**
 * @MongoDB\String
 */
private $name;

/**
 * @MongoDB\String
 */
private $description;

/**
 * @MongoDB\ReferenceMany(targetDocument="Tag")
 */
protected $tags;

/**
 * @MongoDB\ReferenceOne(targetDocument="Category")
 */
private $category;

/**
 * @MongoDB\ReferenceOne(targetDocument="DataRepository")
 */
private $dataRepository;

/**
 * @MongoDB\file
 */
private $logo;

/**
 * @MongoDB\Boolean
 */
private $available;

And here is the formType for my "Resource" document :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name');
    $builder->add('description');
    $builder->add('logo', 'file');
    $builder->add('available', 'checkbox');
    $builder->add('category', 'document', array(
        'class' => 'AppBundle:Category'
    ));
    $builder->add('tags', 'document', array(
        'class' => 'AppBundle:Tag',
        'multiple' => 'true'
    ));
    $builder->add('dataRepository', "document", array(
        'class' => 'PortailCatalogBundle:DataRepository'
    ));
}

The generated form is perfect but when I submit it, it doesn't seem to hydrate my entity as excepted. Here is the request :

["request"]=>
object(Symfony\Component\HttpFoundation\ParameterBag)#7 (1) {
    ["parameters":protected]=>
    array(1) {
      ["resource"]=>
      array(7) {
        ["name"]=>
        string(14) "Ressource N°1"
        ["description"]=>
        string(28) "Juste un test parmi d'autres"
        ["available"]=>
        string(1) "1"
        ["category"]=>
        string(24) "551c02b5a246cb287c09bcde"
        ["tags"]=>
        array(2) {
          [0]=>
          string(24) "551c02b5a246cb287c09bcdc"
          [1]=>
          string(24) "551d0ac2a246cbe40809bc97"
        }
        ["dataRepository"]=>
        string(24) "551d650aa246cb700e09bc9d"
        ["_token"]=>
        string(40) "4adfcc295b2305c784347311d3bbad55c260c6ed"
      }
    }
  }

All those objectIds are the right ones but when submitted, the form generates errors for those 3 documents with the error " This value is not valid. "

I found the queries done right after the form submission :

db.category.find({ "_id": ObjectId("551e66a2a246cba51909bc9c") });
db.tag.find({ "_id": ObjectId("551e66a2a246cba51909bc9d") });
db.dataRepository.find({ "_id": ObjectId("551e66a2a246cba51909bc9e") });

As you can see, the objectIds don't match. I feel like i messed up the formType definition part.

I didn't find any documentation for this kind of form in the DoctrineMongoDBBundle ... Any idea or documentation link ?

Thank you.

There wasn't anything wrong in my source code. I did a composer update of the doctrine/doctrine-odm bundle and the form worked as intended (apparently a commit of the bundle was added).

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