2017-03-28 54 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