php - Symfony search-box issue -
i've issue make search-box symfony , doctrine. use 2.8 version of symfony.
my issue :
entitymanager#persist() expects parameter 1 entity object, array given. 500 internal server error - orminvalidargumentexception
so, type search-box type :
/** * {@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' )); }
and controller code after edit :
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); }
edit : 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 think not solution of issue. then, can tell me wrong ? need search-box entity search-box ?
thanks !
edit :
stack trace
in src\sb\mainbundle\repository\ouvragesrepository.php @ line 130 - public function getouvragename($titre){ $query = $this->getentitymanager()->createquery( "select o sbmainbundle:ouvrages o o.titreouvrage = $titre order o.id desc " ); return $query->getresult(); @ errorhandler ->handleerror ('8', 'array 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 @ line 130 + @ ouvragesrepository ->getouvragename (array('searchbox' => 'flammarion')) in src\sb\mainbundle\controller\maincontroller.php @ line 35 + @ maincontroller ->indexaction (object(request)) @ call_user_func_array (array(object(maincontroller), 'indexaction'), array(object(request))) in vendor\symfony\symfony\src\symfony\component\httpkernel\httpkernel.php @ line 144 + @ httpkernel ->handleraw (object(request), '1')
in vendor\symfony\symfony\src\symfony\component\httpkernel\httpkernel.php @ line 64 + @ httpkernel ->handle (object(request), '1', true) in vendor\symfony\symfony\src\symfony\component\httpkernel\dependencyinjection\containerawarehttpkernel.php @ line 69 + @ containerawarehttpkernel ->handle (object(request), '1', true) in vendor\symfony\symfony\src\symfony\component\httpkernel\kernel.php @ line 185 + @ kernel ->handle (object(request)) in web\app_dev.php @ line 28 +
new edit :
some mandatory parameters missing ("titreouvrage") generate url route "sb_main_ouvrage".
sb_main_ouvrage: path: /ouvrage/{titreouvrage} defaults: { _controller: sbmainbundle:main:ouvrage}
stack trace
in app\cache\dev\classes.php @ line 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 missing ("%s") generate url route "%s".', implode('", "', array_keys($diff)), $name));} $url =''; $optional = true; @ 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 @ line 92 + @ appdevdebugprojectcontainerurlgenerator ->generate ('sb_main_ouvrage', array(), '1') in app\cache\dev\classes.php @ line 1286 + @ router ->generate ('sb_main_ouvrage', array(), '1') in vendor\symfony\symfony\src\symfony\bundle\frameworkbundle\controller\controller.php @ line 52 + @ controller ->generateurl ('sb_main_ouvrage') in src\sb\mainbundle\controller\maincontroller.php @ line 49 + @ maincontroller ->indexaction (object(request)) @ call_user_func_array (array(object(maincontroller), 'indexaction'), array(object(request))) in vendor\symfony\symfony\src\symfony\component\httpkernel\httpkernel.php @ line 144 + @ httpkernel ->handleraw (object(request), '1') in vendor\symfony\symfony\src\symfony\component\httpkernel\httpkernel.php @ line 64 + @ httpkernel ->handle (object(request), '1', true) in vendor\symfony\symfony\src\symfony\component\httpkernel\dependencyinjection\containerawarehttpkernel.php @ line 69 + @ containerawarehttpkernel ->handle (object(request), '1', true) in vendor\symfony\symfony\src\symfony\component\httpkernel\kernel.php @ line 185 + @ kernel ->handle (object(request)) in web\app_dev.php @ line 28
and controller mapping page :
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); }
your form has no entity (or multiple) means $form->getdata() return array. and, error says, cannot pass array persist() method.
if goal save each search sent in db need create entity searchbox (or whatever want call it), , set data_class accordingly.
if don't want store search then, won't need call handler. (because nothing except call persist()) , don't need entity, check how handle submit without data_class
btw. in handler call $form->isvalid() without $form->issubmitted() , code
if ($formhandler->process()){ return $this->redirect($this->generateurl('')); } if ($form->issubmitted() && $form->isvalid()) { // never come here, because $formhandler->process() return true if form valid, , leave indexaction }
hope may helps you.
edit
the data pass repository is
$searchboxvalue = $form->getdata();
this return mark searchboxvalue
array, because ask form data.
but want specific search field data, need do
$searchboxvalue = $form->get('searchbox')->getdata();
note recommend use querybuilder, or @ least protect query against sql injections.
e.g
public function getouvragename($titre){ $query = $this->getentitymanager()->createquery( "select o sbmainbundle:ouvrages o o.titreouvrage = :titre order o.id desc " ) ->setparameter('titre', $titre); return $query->getresult(); }
Comments
Post a Comment