2016-02-16 47 views
0

我是Symfony2的新手,嘗試使用Lexik Filter包;我有兩個實體(家長培訓)以nm(多對多)關係關聯,如下定義,我試圖按名稱過濾父母名單:`tuto \ LexikTestBundle \ Entity \ Parents:Lexik Filter-:參數1傳遞給Symfony Component Form FormRenderer :: searchAndRenderBlock()

type: entity 
table: Parents 
repositoryClass: tuto\LexikTestBundle\Repository\ParentRepository 
id: 
    id: 
     type: integer 
     generator: 
      strategy: AUTO 
fields: 
    Firstname: 
     type: string 
     length: 50 
    Lastname: 
     type: string 
     length: 50 
    DOB: 
     type: datetime 
    Email: 
     type: string 
     length: 50 
manyToMany: 
    Trainings: 
     targetEntity: Training 
     mappedBy: parents` 

我下面的教程與版本3.0.8工作,並定義了以下自定義過濾器類型:

namespace tuto\LexikTestBundle\Form\Filter; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 


class MyParentsType extends AbstractType 
{ 
    /** 
    * Returns the name of this type. 
    * 
    * @return string The name of this type 
    */ 
    public function getName() 
    { 
     return 'parents_filter'; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('Firstname', 'filter_text'); 
     $builder->add('Lastname', 'filter_text'); 

    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(
      [ 
       'csrf_protection' => false, 
       'validation_groups' => ['filtering'] // avoid NotBlank() constraint-related message 
      ] 
     ); 
    } 
} 

並在控制器中執行以下FilterAction:

public function indexAction(Request $request) 
{ 


    $em = $this->getDoctrine()->getManager(); 
    $entities = $em->getRepository('tutoLexikTestBundle:Parents')->findAll(); 

    $form = $this->testFilterAction($request); 

    return $this->render('tutoLexikTestBundle:Parents:index.html.twig', [ 
     'entities' => $entities, 
     'form' => $form, 
    ] 
    ); 
} 

public function testFilterAction(Request $request) 
{ 
    $form= $this->get('form.factory')->create(new MyParentsType()); 

    if($request->query->has($form->getName())) { 
     // manually bind values from the request 
     $form->submit($request->query->get($form->getName())); 

     $queryBuilder = $this->get('doctrine.orm.entity_manager') 
      ->getRepository('LexikTestBundle:Parents') 
      ->createQueryBuilder('e'); 

     //build the query from the given object 
     $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form,$queryBuilder); 
     var_dump($queryBuilder->getDql()); 
    } 

    return $this->render(
     'tutoLexikTestBundle:Default:testFilter.html.twig', 
     ['form'=>$form->createView()] 
    ); 
} 

的樹枝實木emented是

<form method="get" action="."> 
 
    {{ form_rest(form) }} 
 
    <input type="submit" name="submit-filter" value="filter" /> 
 
</form>

我已經走線槽以下答案,似乎沒有同樣的錯誤:

但我仍然面對捕致命錯誤:傳遞給Symfony的\分量\形式參數1 \ FormRenderer :: searchAndRenderBlock()必須的Symfony \分量\表格\ FormView控件,實例的實例Symfony \ Component \ HttpFoundation \ Response給出任何提示都會受到歡迎。

回答

1

你應該呈現表單視圖,而不是一個表單對象

public function indexAction(Request $request) 
    { 
     $em = $this->getDoctrine()->getManager(); 
     $entities = $em->getRepository('tutoLexikTestBundle:Parents')->findAll(); 

     $form = $this->testFilterAction($request); 

     return $this->render('tutoLexikTestBundle:Parents:index.html.twig', [ 
      'entities' => $entities, 
      'form' => $form->createView(), 
     ] 
     ); 
    } 

你testFilterAction返回一個Response對象,而不是一個表單對象或視圖 請如下更改方法:

注意:你可以在你的表格模板中嵌套

tutoLexikTestBundle:Parents:index.html.twig' view 

public function testFilterAction(Request $request) 
{ 
    $form= $this->get('form.factory')->create(new MyParentsType()); 

    if($request->query->has($form->getName())) { 
     // manually bind values from the request 
     $form->submit($request->query->get($form->getName())); 

     $queryBuilder = $this->get('doctrine.orm.entity_manager') 
      ->getRepository('LexikTestBundle:Parents') 
      ->createQueryBuilder('e'); 

     //build the query from the given object 
     $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form,$queryBuilder); 
     var_dump($queryBuilder->getDql()); 
    } 

    return $form; // return form object 
    ); 
} 
+0

我確實試過了,但得到了一個'Ca將未定義的方法Symfony \ Component \ HttpFoundation \ Response :: createView()' –

+0

是的這是預期的行爲,請編輯您的方法如上 – CodeIsLife

相關問題