0

登録フォーム時に値を保存する必要があります。コントローラーにはこれがありました:

<?php
$heritageBuilder = $this->createFormBuilder($heritage)
    ->add('category', 'choice')
    ->add('designation', 'text')
    ->add('value', 'text');
$heritageFormBuilder = $heritageBuilder->getForm();
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
    $heritageFormBuilder->bindRequest($request);
...
}
?>

私はこれを受け取りました:

array(2) {
      ["form"]=>
      array(4) {
        ["category"]=>
        string(1) "4"
...

カテゴリは別のテーブル型HeritageCategoryへの外部キーであるため、機能しませんでした。 したがって、受け取った値を次のように変更しました。正しい形式は次のとおりです。

<?php
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
    $post = $request->request->get('form');
    $post['category'] = $em->getRepository('PrismeMigrationBundle:HeritageCategory')->find((int)$post['category']);
    $request->request->set('form', $post);

    $heritageFormBuilder->bindRequest($request);
}
?>

私は、私のリクエストの多くの変更を高く評価しています:

["form"]=>
      array(4) {
        ["category"]=>
        object(Test\MonBundle\Entity\HeritageCategory)#427 (6) {
          ["id":"Test\MonBundle\Entity\HeritageCategory":private]=>
          int(4)
          ["parent":"Test\MonBundle\Entity\HeritageCategory":private]=>
          int(3)
          ["code":"Test\MonBundle\Entity\HeritageCategory":private]=>
          string(0) ""
          ["name":"Test\MonBundle\Entity\HeritageCategory":private]=>
          string(21) "Résidence principale"
          ["bg":"Test\MonBundle\Entity\HeritageCategory":private]=>
          int(4)
          ["bd":"Test\MonBundle\Entity\HeritageCategory":private]=>
          int(5)
        }

しかし、私はこのエラーを受け取ります: タイプ「スカラー」の予期された引数、「Prisme\MigrationBundle\Entity\HeritageCategory」が与えられました

BindRequest の修正が正しく行われているため、どうすればよいですか?

前もって感謝します

4

1 に答える 1

2

これですべてが解決するかどうかはわかりませんが、それが始まりであることを願っています...

それ以外の:

$heritageBuilder = $this->createFormBuilder($heritage)
    ->add('category', 'choice')
    ->add('designation', 'text')
    ->add('value', 'text');

試す:

$heritageBuilder = $this->createFormBuilder($heritage)
    ->add('category', 'entity')
    ->add('designation', 'text')
    ->add('value', 'text');

「choice」タイプは、スカラー情報の単純な配列 (「男性」/「女性」など) 用に設計されています。しかし、「カテゴリ」はスカラーではありません。これは完全なエンティティであるため、ここでは「エンティティ」タイプの方が理にかなっています。

エンティティ フィールド タイプに関するドキュメントは次のとおりです。

http://symfony.com/doc/2.0/reference/forms/types/entity.html

ああ、HeritageCategory に toString() メソッドがあることを確認してください。フォームはラベルを表示するためにそれを必要とします。

于 2012-09-19T19:22:22.230 に答える