2015-06-09 27 views
2

我有三個項目實體:Book,Author和Theme。有關信息,作者和主題與許多對一/一對多的書聯繫在一起。一本書有一個或多個作者和一個或多個主題。 而且我已經生成了關於基礎函數(添加,編輯和刪除)和默認視圖/表單的論文。Symfony 2 - 用於搜索表單的Differents字段比列表

但我需要做一些搜索功能,三正是:

  • 搜索隨書標題
  • 搜索圖書的一部分,一個作者
  • 搜索簿與一個或多個主題。

我編碼的三個功能和工作良好,但我有一個關於在搜索表單中使用的字段的問題。

第一個是正確的,它表明我們把標題和結果是正確的圖書列表,但兩個人一個輸入字段...

因爲關係的字段是下拉列表,其中用戶選擇一個項目和搜索它,結果是正確的,但我希望第二個有搜索作者姓名和最後顯示覆選框的輸入字段,所以我做了研究,但我不明白爲什麼在我的表單類型,我無法改變我想要的領域。

這裏我有兩種形式類型:

搜索一個作者:我只是想顯示一個輸入字段,而不是一個列表,但一次比一次錯誤

<?php namespace Projet\BibliothequeBundle\Form; 

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

class BookByAuthorType extends AbstractType 
{ 
    /** 
* @param FormBuilderInterface $builder 
* @param array $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('authors') 
    ; 
} 

/** 
* @param OptionsResolverInterface $resolver 
*/ 
public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'Projet\BibliothequeBundle\Entity\Book' 
    )); 
} 

/** 
* @return string 
*/ 
public function getName() 
{ 
    return 'authors'; 
} 

} 

搜索簿與一個或多個主題:我試圖通過鏈接「主題」數組來顯示chechbox,但不起作用。

<?php namespace Projet\BibliothequeBundle\Form; 

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

class BookByThemeType extends AbstractType 
{ 
    /** 
* @param FormBuilderInterface $builder 
* @param array $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    // $themes = array(); 
    // $themes=$builder->add('themes'); 
    $builder 
     ->add('themes', 'choice', [ 
      'choices' => 'themes', 
      'multiple' => true, 
      'expanded' => true 
     ]) 
    ; 
} 

/** 
* @param OptionsResolverInterface $resolver 
*/ 
public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'Projet\BibliothequeBundle\Entity\Book' 
    )); 
} 

/** 
* @return string 
*/ 
public function getName() 
{ 
    return 'themes'; 
} 
} 

更新:我的BookController函數可能是我的問題的根源。

// AUTHOR SEARCH FUNCTIONS 
public function SearchByAuthorAction() { 
    $entity = new Book(); 
    $form = $this->CreateSearchAuthorForm($entity); 
    return $this->render('ProjetBibliothequeBundle:Book:searchbyauthor.html.twig', array( 
     'form' => $form->createView(), 
    )); 
} 

private function CreateSearchAuthorForm(Book $entity) { 
    $form = $this->createForm(new BookByAuthorType(), $entity, array(
     'action' => $this->generateUrl('book_submit_search_by_author'), 
     'method' => 'POST', 
    )); 

    $form->add('submit', 'submit', array('label' => 'Search')); 

    return $form; 
} 

public function SubmitSearchByAuthorAction(Request $request) { 
    $entity = new Book(); 
    $form = $this->CreateSearchAuthorForm($entity); 
    $form->handleRequest($request); 

    $author = $form->get('authors')->getData(); 

    $repository = $this->getDoctrine() 
         ->getEntityManager() 
         ->getRepository('ProjetBibliothequeBundle:Book'); 
    $Booklist = $repository->findByAuthor($author); 

    return $this->render('ProjetBibliothequeBundle:Book:resultbyauthor.html.twig', 
         array('Booklist' => $Booklist)); 
} 

//THEME SEARCH FUNCTIONS 
public function SearchByThemeAction() { 
    $entity = new Book(); 
    $form = $this->CreateSearchThemeForm($entity); 
    return $this->render('ProjetBibliothequeBundle:Book:searchbytheme.html.twig', array( 
     'form' => $form->createView(), 
    )); 
} 

private function CreateSearchThemeForm(Livre $entity) { 
    $entities = $this->getDoctrine()->getManager()->getRepository('ProjetBibliothequeBundle:Book')->findAll(); 
    $form = $this->createForm(new BookByThemeType(), $entity, array(
     'action' => $this->generateUrl('book_submit_search_by_theme'), 
     'method' => 'POST', 
    )); 

    $form->add('submit', 'submit', array('label' => 'Search')); 

    return $form; 
} 

public function SubmitSearchByThemeAction(Request $request) { 
    $entity = new Book(); 
    $form = $this->CreateSearchThemeForm($entity); 
    $form->handleRequest($request); 

    $theme = $form->get('themes')->getData(); 

    $repository = $this->getDoctrine() 
         ->getEntityManager() 
         ->getRepository('ProjetBibliothequeBundle:Book'); 
    $Booklist = $repository->findByTheme($theme); 

    return $this->render('ProjetBibliothequeBundle:Book:resultbytheme.html.twig', 
         array('Booklist' => $Booklist)); 
} 

回答

0

沒有必要設置data_classGET操作。只要創建一個基本形式,可能是這樣的:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('themes', 'choice', [ 
      'choices' => 'themes', 
      'multiple' => true, 
      'expanded' => true 
     ]) 
    ; 
} 

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(['method' => 'GET', 'csrf_protection' => false]); 
} 
從網址你的動作捕捉值

,它們傳遞給findBy方法或手動生成您查詢:

public function someAction(Request $request) 
{ 
    ... 

    $form->createForm(new FORM_TYPE, null); 

    .... 

    $filters = $request->query->get($form->getName()); 

    $result = $entityManager->getRepository(REPOSITORY)->findBy($filters, $orderBy, $limit, $offset); 

    ... 
} 
+0

我有一個錯誤:「捕致命錯誤:傳遞給Symfony \ Component \ Form \ Extension \ Core \ ChoiceList \ SimpleChoiceList :: __ construct()的參數1必須是類型數組,字符串給定,在/.../symfony/vendor/symfony/symfony/中調用第172行的src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php,並在/.../symfony/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ SimpleChoiceList.php行45「 – Tenebros

+0

是的我在BookRepository中使用了FindByTitle,FindByThemes和FindByAuthors三種方法。 – Tenebros

+0

我用我的BookController函數更新了我的帖子。 – Tenebros