Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony搜索框问题_Php_Symfony_Doctrine Orm_Search Box - Fatal编程技术网

Php Symfony搜索框问题

Php Symfony搜索框问题,php,symfony,doctrine-orm,search-box,Php,Symfony,Doctrine Orm,Search Box,我有一个问题,需要用Symfony和doctor创建一个搜索框。我使用Symfony的2.8版本 我的问题是: EntityManager#persist()希望参数1是给定数组的实体对象。500内部服务器错误-ORMInvalidArgumentException 这是我的搜索框类型的类型: /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) {

我有一个问题,需要用Symfony和doctor创建一个搜索框。我使用Symfony的2.8版本

我的问题是:

EntityManager#persist()希望参数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();
}
我已经看过了,但我认为这不是解决我问题的办法。然后,有人能告诉我怎么了?即使有搜索框,我也需要搜索框实体吗

谢谢你的帮助

编辑:

堆栈跟踪

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') 
在第64行的vendor\symfony\symfony\src\symfony\Component\HttpKernel\HttpKernel.php中+ 在HttpKernel->handle(对象(请求),'1',true) 在第69行的vendor\symfony\symfony\src\symfony\Component\HttpKernel\DependencyInjection\containerWarehttpkernel.php中+ 在ContainerWarehttpkernel->handle(对象(请求),'1',true) 在第185行的vendor\symfony\symfony\src\symfony\Component\HttpKernel\Kernel.php中+ 在内核->句柄(对象(请求)) 在web\app_dev.php的第28行+

新编辑:

缺少一些必需参数(“TitreOutrage”)来生成路由“sb_main_ouvrage”的URL

堆栈跟踪

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') 
第911行的app\cache\dev\classes.php中

$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);
}
表单没有实体(或多个实体),这意味着$form->getData()将返回一个数组。并且,正如错误所说,不能将数组传递给persist()方法

如果您的目标是保存数据库中发送的每个搜索,那么您需要创建一个实体搜索框(或任何您想称之为实体搜索框的名称),并相应地设置data_类

如果不想存储搜索,则不需要调用处理程序。(因为它除了调用persist()之外什么都不做) 你不需要实体

顺便说一句,在处理程序中调用$form->isValid(),而不调用$form->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();
这将以数组形式返回mark
searchBoxValue
,因为您需要所有表单数据

但您想要的是获得特定的搜索字段数据,您需要执行以下操作

$searchBoxValue = $form->get('searchBox')->getData();
注意,我建议使用queryBuilder,或者至少保护您的查询不受sql注入的影响

e、 g

表单没有实体(或多个实体),这意味着$form->getData()将返回一个数组。并且,正如错误所说,不能将数组传递给persist()方法

如果您的目标是保存数据库中发送的每个搜索,那么您需要创建一个实体搜索框(或任何您想称之为实体搜索框的名称),并相应地设置data_类

如果不想存储搜索,则不需要调用处理程序。(因为它除了调用persist()之外什么都不做) 你不需要实体

顺便说一句,在处理程序中调用$form->isValid(),而不调用$form->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();
这将以数组形式返回mark
searchBoxValue
,因为您需要所有表单数据

但您想要的是获得特定的搜索字段数据,您需要执行以下操作

$searchBoxValue = $form->get('searchBox')->getData();
注意,我建议使用queryBuilder,或者至少保护您的查询不受sql注入的影响

e、 g


最好有FormHandler的代码,错误可能来自那里。您的错误谈到persist()调用,但我在代码中没有看到。我添加了formHandler,但我的ap的许多表单都需要它。我需要为我的搜索框做一个新的吗?最好有你的FormHandler的代码,错误可能来自那里。您的错误谈到persist()调用,但我在代码中没有看到。我添加了formHandler,但我的ap的许多表单都需要它。是否需要为我的搜索框创建一个新的搜索框?我尝试了您的解决方案,但我遇到了一个新问题:执行“SELECT o0.is Ou delete AS is Ou delete0,[…]FROM Ovrages o0”?其中o0.titre ouvrage=?”和参数[“lombres”]:SQLSTATE[HY093]:无效参数号:未定义参数。我在谷歌上寻找,但任何东西都能解决我的问题。你可能想向我们展示你选择的方法的代码,我想你有一个stacktrace,可以告诉你哪个方法存在错误(或者至少哪个方法无效),并在你的问题中添加这个方法的代码。我可以向你展示我在我的数据库中寻找的请求,也许这有助于您理解我的问题。对我来说,问题在于存储库的方法
getOuvrageName
。我们需要查看一些代码来帮助您。我的代码:`public function getOuvrageName($searchBoxValue){$query=$this->createQueryBuilder('o')->where('o.titreOutrage=:searchBoxValue')->setParameter('searchBoxValue',$searchBoxValue');return$query->getQuery->getQuery()->getResult();}`我尝试了您的解决方案,但我遇到了一个新问题:从ouvrages o0执行“SELECT o0.is_delete AS is is_delete0,[…]时发生异常,其中o0.titre_ouvrage=?”带有参数[“lombres”]:SQLSTATE[HY093]:无效参数号:未定义参数。我在谷歌上寻找,但任何东西都能解决我的问题。你可能想向我们展示你选择的方法的代码,我想你有一个stacktrace,可以告诉你哪个方法存在错误(或者至少哪个方法无效),并在你的问题中添加这个方法的代码。我可以向你展示我在我的数据库中寻找的请求,也许这有助于您理解我的问题。对我来说,问题在于存储库的方法
getOuvrageName
。我们需要查看一些代码来帮助您。我的代码:`public function getOuvrageName($searchBoxValue){$query=$this->createQueryBuilder('o')->where('o.titreOutrage=:searchBoxValue'))