2017-03-28 56 views
0

我有一個問題,使用Symfony和Doctrine的搜索框。我使用Symfony的2.8版本。Symfony搜索框問題

我的問題是:

的EntityManager#堅持()預計參數1是一個實體對象,給定陣列。 500內部服務器錯誤 - ORMInvalidArgumentException

所以,這是我爲我的搜索框中鍵入類型:

/** 
* {@inheritdoc} 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) { 

    $builder->add('searchBox',SearchType::class,array('label'=>'Vous cherchez : Un auteur ? Un éditeur ? Un ouvrage ?','attr'=>array('class'=>'form-control'))); 

} 

/** 
* {@inheritdoc} 
*/ 
public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class1' => 'SB\MainBundle\Entity\Ouvrages', 
     'data_class2' => 'SB\MainBundle\Entity\Auteurs', 
     'data_class3' => 'SB\MainBundle\Entity\Editeurs' 
    )); 
} 

這是編輯後,我的控制器代碼:

public function indexAction(Request $request) { 
    $ouvragesVentes = $this->getDoctrine()->getRepository('SBMainBundle:Ouvrages')->getOuvragesVentes(); 
    $ouvragesEchanges = $this->getDoctrine()->getRepository('SBMainBundle:Ouvrages')->getOuvragesEchanges(); 
    $categories = $this->getDoctrine()->getRepository('SBMainBundle:Categories')->getAllCats(); 

    $form = $this->createForm(SearchBoxType::class); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 

     $searchBoxValue = $form->getData(); 

     $bookName = $this->getDoctrine()->getRepository('SBMainBundle:Ouvrages')->getOuvrageName($searchBoxValue); 
     $editorName = $this->getDoctrine()->getRepository('SBMainBundle:Editeurs')->getEditeurName($searchBoxValue); 
     $autorName = $this->getDoctrine()->getRepository('SBMainBundle:Auteurs')->getAuteurName($searchBoxValue); 

     //if author 
     if ($searchBoxValue == $autorName){ 
      return $this->redirect($this->generateUrl('sb_main_auteur')); 
     } 
     //if editor 
     if ($searchBoxValue == $editorName){ 
      return $this->redirect($this->generateUrl('sb_main_editeur')); 
     } 
     //if book name 
     if ($searchBoxValue == $bookName){ 
      return $this->redirect($this->generateUrl('sb_main_ouvrage')); 
     } 
    } 

    $datas = array('categories'=>$categories,'form'=>$form->createView(),'ouvragesEchanges'=>$ouvragesEchanges,'ouvragesVentes'=>$ouvragesVentes); 
    return $this->render('SBMainBundle:Main:index.html.twig',$datas); 
} 

編輯:我的formHandler

protected $request; 
protected $form; 
protected $em; 


//faire une injection de dépendance avec Request,Manager,Form(Objet) 
public function __construct(Request $request,EntityManager $em, Form $form){ 
    $this->request = $request; 
    $this->em = $em; 
    $this->form = $form; 
} 

//vérifie si le formulaire est soumis et valide 
public function process(){ 

    if ($this->request->getMethod() == "POST"){ 
     //récupération des données de la requête de la superglobale $_POST 
     $this->form->handleRequest($this->request); 
     //si ok, on appel onSuccess() 
     if ($this->form->isValid()){ 
      $this->onSuccess($this->form->getData()); 
      return true; 
     } 
    } 
    return false; 

} 

//si formulaire soumis et valide, on presiste l'objet (enregistre dans la DB) 
public function onSuccess($object){ 
    //on persiste dans la DB via le manager Doctrine 
    $this->em->persist($object); 
    $this->em->flush(); 
} 

我'我看看here但我認爲不是我的問題的解決方案。那麼,有人可以告訴我什麼是錯的?即使使用搜索框,我是否還需要搜索框實體?

感謝您的幫助!

編輯:

堆棧跟蹤

in src\SB\MainBundle\Repository\OuvragesRepository.php at line 130 - 

public function getOuvrageName($titre){ 
    $query = $this->getEntityManager()->createQuery(
     "SELECT o FROM SBMainBundle:Ouvrages o WHERE o.titreOuvrage = $titre ORDER BY o.id DESC " 
    ); 
    return $query->getResult(); 


at ErrorHandler ->handleError ('8', 'Array to string conversion',  'C:\wamp\www\switchbook\src\SB\MainBundle\Repository\OuvragesRepository.php', '130', array('titre' => array('searchBox' => 'flammarion'))) 
in src\SB\MainBundle\Repository\OuvragesRepository.php at line 130 + 
at OuvragesRepository ->getOuvrageName (array('searchBox' => 'flammarion')) 
in src\SB\MainBundle\Controller\MainController.php at line 35 + 
at MainController ->indexAction (object(Request)) 
at call_user_func_array (array(object(MainController), 'indexAction'), array(object(Request))) 
in vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php at line 144 + 
at HttpKernel ->handleRaw (object(Request), '1') 

在供應商\ symfony的\ symfony的\ SRC \的Symfony \元器件\ HttpKernel \ HttpKernel.php在管線64 + 在HttpKernel - >手柄(對象(請求),'1',true) 在Container \ Symfony \ symfony \ src \ Symfony \ Component \ HttpKernel \ DependencyInjection \ ContainerAwareHttpKernel.php在69行+ 位於ContainerAwareHttpKernel-> handle(object(Request),'1' true) in vendor \ symfony \ symfony \ src \ Symfony \ Component \ HttpKernel \ Kernel。 PHP在行185 + 在內核 - >手柄(對象(請求)) 在網絡\ app_dev.php在第28行+

新編輯:

一些強制性的參數丟失( 「titreOuvrage」 )爲路由「sb_main_ouvrage」生成一個URL。

sb_main_ouvrage: 
path:  /ouvrage/{titreOuvrage} 
defaults: { _controller: SBMainBundle:Main:ouvrage} 

堆棧跟蹤

在應用

\緩存\ dev的\在行classes.php 911

$variables = array_flip($variables); 
$mergedParams = array_replace($defaults, $this->context->getParameters(), $parameters); 
if ($diff = array_diff_key($variables, $mergedParams)) {throw new MissingMandatoryParametersException(sprintf('Some mandatory parameters are missing ("%s") to generate a URL for route "%s".', implode('", "', array_keys($diff)), $name));} 
$url =''; 
$optional = true; 

at UrlGenerator ->doGenerate (array('titreOuvrage'), array('_controller' => 'SB\MainBundle\Controller\MainController::ouvrageAction'), array(), array(array('variable', '/', '[^/]++', 'titreOuvrage'), array('text', '/ouvrage')), array(), 'sb_main_ouvrage', '1', array(), array()) 
in app\cache\dev\appDevDebugProjectContainerUrlGenerator.php at line 92 + 
at appDevDebugProjectContainerUrlGenerator ->generate ('sb_main_ouvrage', array(), '1') 
in app\cache\dev\classes.php at line 1286 + 
at Router ->generate ('sb_main_ouvrage', array(), '1') 
in vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php at line 52 + 
at Controller ->generateUrl ('sb_main_ouvrage') 
in src\SB\MainBundle\Controller\MainController.php at line 49 + 
at MainController ->indexAction (object(Request)) 
at call_user_func_array (array(object(MainController), 'indexAction'), array(object(Request))) 
in vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php at line 144 + 
at HttpKernel ->handleRaw (object(Request), '1') 
in vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php at line 64 + 
at HttpKernel ->handle (object(Request), '1', true) 
in vendor\symfony\symfony\src\Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel.php at line 69 + 
at ContainerAwareHttpKernel ->handle (object(Request), '1', true) 
in vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php at line 185 + 
at Kernel ->handle (object(Request)) 
in web\app_dev.php at line 28 

而且我對映射頁面控制器:

public function ouvrageAction(Ouvrages $ouvrages){ 
    $ouvrage = $ouvrages->getTitreOuvrage(); 
    $categories = $this->getDoctrine()->getRepository('SBMainBundle:Categories')->getAllCats(); 
    $ouvragesVentes = $this->getDoctrine()->getRepository('SBMainBundle:Ouvrages')->getOuvragesVentesByName($ouvrage); 
    $ouvragesEchanges = $this->getDoctrine()->getRepository('SBMainBundle:Ouvrages')->getOuvragesEchangesByName($ouvrage); 
    $datas = array('ouvragesVentes'=>$ouvragesVentes,'ouvragesEchanges'=>$ouvragesEchanges,'categories'=>$categories,'ouvrages'=>$ouvrages); 
    return $this->render('SBMainBundle:Main:ouvrage.html.twig',$datas); 
} 
+1

這將是很好有你FormHandler的代碼,錯誤可能來自那裏。你的錯誤討論了persist()調用,但是我的代碼中沒有看到。 – DFayet

+0

我添加了我的formHandler,但我需要它爲我的許多窗體。是否需要我爲我的搜索框添加一個新的? – Tirkal

回答

0

您的表單沒有實體(或多個),這意味着您的$ form-> getData()將返回一個ar射線。而且,如錯誤所述,您不能將數組傳遞給persist()方法。

如果您的目標是保存發送到您的數據庫中的每個搜索,您需要創建一個實體搜索框(或任何你想調用它),並相應地設置data_class。

如果你不想存儲搜索,那麼你不需要調用你的處理程序。 (因爲它除了調用persist()以外什麼都沒做) 而且你不需要實體,check how to handle the submit without data_class

Btw。在您的處理程序調用形式 - $> isValid()的無$形式 - > isSubmitted()和驗證碼

if ($formHandler->process()){ 
    return $this->redirect($this->generateUrl('')); 
} 

if ($form->isSubmitted() && $form->isValid()) { 
// You will never come here, because $formHandler->process() will return true if your form is valid, and leave your indexAction 
} 

希望這可以幫助你。

編輯

傳遞給你的資料庫中的數據是

$searchBoxValue = $form->getData(); 

這將返回標記searchBoxValue爲數組,因爲你問所有的表單數據。

但是你想要的是讓特定的搜索領域的數據,你需要做的

$searchBoxValue = $form->get('searchBox')->getData(); 

注意,我會建議使用QueryBuilder的,或者至少保護你的查詢針對SQL注入。

e.g

public function getOuvrageName($titre){ 
    $query = $this->getEntityManager()->createQuery(
     "SELECT o FROM SBMainBundle:Ouvrages o WHERE o.titreOuvrage = :titre ORDER BY o.id DESC " 
    ) 
    ->setParameter('titre', $titre); 
    return $query->getResult(); 
} 
+0

我嘗試了您的解決方案,並且發現了一個新問題: **執行'SELECT o0_.is_delete AS is_delete0,[FROM] FROM oo_ WHERE o0_.titre_ouvrage =?'時發生異常帶參數[「lombres」]: SQLSTATE [HY093]:參數號無效:參數未定義**。我在谷歌上尋找,但任何解決我的問題。 – Tirkal

+0

您可能想向我們展示您選擇的方法的代碼,我認爲您有一個堆棧跟蹤可以告訴您哪種方法存在該錯誤(或至少哪個方法無效),並在您的問題中添加此方法的代碼。 – DFayet

+0

我可以向您顯示我在我的數據庫中查找的請求,也許這有助於您理解我的問題。 – Tirkal