2011-10-18 4 views
24

フォームに「エンティティ」タイプのフィールドを表示し、コントローラから渡す引数に基づいてこのエンティティタイプをフィルタリングしたい場合、どうすればいいですか?コントローラからタイプsymfony2にデータを渡します

//PlumeOptionsType.php 
public function buildForm(FormBuilder $builder, array $options) 
{ 
    $builder->add('framePlume', 'entity', array(
     'class' => 'DessinPlumeBundle:PhysicalPlume', 
     'query_builder' => function(EntityRepository $er) { 
           return $er->createQueryBuilder('pp') 
            ->where("pp.profile = :profile") 
            ->orderBy('pp.index', 'ASC') 
            ->setParameter('profile', ????) 
           ; 
          }, 

    )); 
} 

public function getName() 
{ 
    return 'plumeOptions'; 
} 

public function getDefaultOptions(array $options) 
{ 
    return array(
      'data_class'  => 'Dessin\PlumeBundle\Entity\PlumeOptions', 
      'csrf_protection' => true, 
      'csrf_field_name' => '_token', 
      // a unique key to help generate the secret token 
      'intention'  => 'plumeOptions_item', 
    ); 
} 
} 

とコントローラの内部で、私はフォームを作成:

i have that argument that i need to pass in my action code: 
$profile_id = $this->getRequest()->getSession()->get('profile_id'); 
... 
and then i create my form like this 
$form = $this->createForm(new PlumeOptionsType(), $plumeOptions); 

$ plumeOptionsが持続するだけのクラスです。しかし、それはPhysicalPlumeと呼ばれる別のクラスとの1対1の関係を持っています。今、私は私のコードで 'framePlume'を表示するには、私はフィルタリングされたPhysicalPlumeエンティティを表示したい。

+0

は既に回答済み... チェックhttp://stackoverflow.com/questions/6716776/symfony-2-how-to-pass-data-to-for mbuilder – xeon

答えて

38

次のようにフォームクラスにパラメータを渡すことができます。そして、

//PlumeOptionsType.php 
protected $profile; 

public function __construct (Profile $profile) 
{ 
    $this->profile = $profile; 
} 

をごbuildFormのquery_builderでそれを使用します。

$profile = $this->profile; 

$builder->add('framePlume', 'entity', array(
    'class' => 'DessinPlumeBundle:PhysicalPlume', 
    'query_builder' => function(EntityRepository $er) use ($profile) { 
          return $er->createQueryBuilder('pp') 
           ->where("pp.profile = :profile") 
           ->orderBy('pp.index', 'ASC') 
           ->setParameter('profile', $profile) 
          ; 
         }, 

)); 

そして最後に、あなたのコントローラで:

// fetch $profile from DB 
$form = $this->createForm(new PlumeOptionsType($profile), $plumeOptions); 
+0

あなたの答えは、私はあなたが正確に何を意味していると思う... まだ、私はあなたが示唆したものを正確にやっているエラーを持っています。 $これを使用しないときはPlumeBundle \フォーム\タイプでオブジェクトコンテキスト で\ PlumeOptionsType.php – xeon

+0

http://pastebin.com/RVLFCxL4 http://pastebin.com/778ygFgR – xeon

+0

http://pastebin.com/RVLFCxL4 http://pastebin.com/778ygFgR http://pastebin.com/q81k8w9A 私の問題はコールバック機能に関連していると思います。私はPlumeOptionsTypeの内部からプロファイルを読むことができますが、内部からは読み取ることができません 'query_builder' => function(EntityRepository $ er) – xeon

4

$plumeOptionsを使用して引数をすべて渡すことができますが、PlumeOptionsTypegetDefaultOptions()を追加すると、オプションのデフォルト値を指定する必要があります。 このメソッドの外観を確認するには、https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.phpを参照してください。

+1

もっと詳しいことは? あなたはもっと具体的になることができますか? – xeon

+0

私のメッセージを編集して、 'getDefaultOptions()'メソッドの詳細を追加しました。 – greg0ire

+0

Okume、PlumeOptionsTypeのgetDefaultOptions()の返された配列にデフォルトのprofile_idを追加しました。 と私は を使用しました - > setParameter( 'profile'、$ options ['profile_id'])... 最初の場所で$ plumeOptionsにprofile_idを渡す方法はありますか? ありがとうございます! – xeon

関連する問題