2017-03-28 10 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(); 
} 

I ' VEのはhereに見えるが、私は私の問題の解決策ではないと思います。誰かが間違っていることを教えてくれる?検索ボックスでも検索ボックスのエンティティが必要ですか?

ありがとうございました!

編集:

スタックトレース\ symfonyの\ symfonyの\ SRC \ symfonyの\コンポーネント\ HttpKernel \ HttpKernel.php HttpKernelのライン64で+ ベンダーで

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') 

- >ハンドル(オブジェクト( (request)、 '1'、true) をベンダーの\ symfony \ symfony \ src \ Symfony \ Component \ HttpKernel \ DependencyInjection \ ContainerAwareHttpKernel.php(ContainerAwareHttpKernel - > handleカーネル\ベンダ\ symfonyの\ symfonyの\ SRC \ symfonyの\コンポーネント\ HttpKernelで )真。ライン185カーネルで+ でPHP - ラインでウェブの\ 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のコードを持っていいだろう。あなたのエラーは、永続的な()呼び出しについて話していますが、あなたのコードには何も見えません。 – DFayet

+0

私は私のformHandlerを追加しますが、私はapのための多くのフォームのためにそれを必要とします。私の検索ボックスに新しいものを作る必要はありますか? – Tirkal

答えて

0

フォームはあなたます$ form-を意味している何のエンティティ(または複数)を持っていません>のgetData()ARを返します。レイ。エラーが言うようにと、あなたが持続()メソッドに配列を渡すことはできません。

あなたの目標は、あなたがエンティティサーチ(またはものは何でもあなたはそれを呼びたい)を作成し、それに応じてdata_classを設定する必要があなたのデシベルで送信された各検索を保存する場合。

検索結果を保存しない場合は、ハンドラを呼び出す必要はありません。 (persist()を呼び出す以外は何もしないため) エンティティは必要ありません。

Btw。あなたのハンドラの中で、あなたはます$ form-> isSubmitted(なし>はisValid())を呼び出します$ form-このコード

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を返します

$searchBoxValue = $form->getData(); 

です。

しかし、あなたが望むことは、特定の検索フィールドデータを取得することで、あなたは私がqueryBuilderを使用することをお勧めします、または少なくともSQLインジェクションに対してクエリを保護する

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

注意を行う必要があります。

例えば

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

あなたの解決策を試してみました。新しい問題が発生しました: ** 'o0_.is_delete AS is_delete0、[...] FROM ouvrages o0_ where o0_.titre_ouvrage =?'の実行中に例外が発生しました。 params ["lombres"]: SQLSTATE [HY093]:無効なパラメータ番号:パラメータが定義されていません**。私はグーグルで探していますが、何か私の問題を解決します。 – Tirkal

+0

あなたのselectメソッドのコードを見せたいと思うかもしれませんが、どのメソッドにバグがあるか(少なくともどちらが無効か)を教えてくれるstacktraceがあり、あなたの質問にこのメソッドのコードを追加すると思います。 – DFayet

+0

私のデータベースで探している私の要求を示すことができます。私の問題を理解するのに役立つかもしれません。 – Tirkal