Search 使用条令和symfony2进行自定义搜索
我想创建一个searchController,它在db中搜索用户在文本框中输入的任何关键字。我看了很多,还有更多 这就是我到目前为止所做的:Search 使用条令和symfony2进行自定义搜索,search,symfony,doctrine,Search,Symfony,Doctrine,我想创建一个searchController,它在db中搜索用户在文本框中输入的任何关键字。我看了很多,还有更多 这就是我到目前为止所做的: public function searchAction(Request $request) { if ($request->getMEthod() == 'GET') { $title = $request->get('Search_term'); //echo "<div
public function searchAction(Request $request) {
if ($request->getMEthod() == 'GET') {
$title = $request->get('Search_term');
//echo "<div class=\"searchText\">Search Results</div><hr/>";
$em = $this->getDoctrine()->getManager();
$Search_terms = explode(' ', $title); //splits search terms at spaces
$query = "SELECT * FROM Entity/Adverts WHERE ";
foreach ($Search_terms as $each) {
// echo $each."<br/>";
$i = 0;
$i++;
if ($i == 1)
$query .= "Adv_title LIKE '%$each%' ";
else
$query .= "OR Adv_title LIKE '%$each%' ";
}
$query = $em->createQuery($query);
$numRow = $query->count();
if ($numRow > 0) {
while ($query->fetch()) {
$repository = $em->getRepository('YCRYcrBundle:Adverts')->findBy(array(
'advTitle' => $title
));
/* echo "<h2><a href='#'> $title</a> </h2>";
echo "$desc <br /> <br />";
echo"<a href='/201308/View/YCR/index.php' class='link-button right'><span>Apply</span></a>"; */
}
}
/* else
echo "none found for \"<b>$SearchTerm </b>\"</br>Check spelling"; */
}
return $this->render('YCRYcrBundle:Search:search.html.twig', array('title' => $title->getAdvTitle()));
}
您需要执行查询(使用execute),否则将无法获得数组。您应该使用查询生成器,而不是DQL字符串连接。阅读关于基本原则用法的原则文档,实体名称空间似乎也不正确 我引述你的话:
$query = $em->createQuery($query);
$numRow = $query->count();
应至少:
$query = $em->createQuery($query);
$numRow = count($query->execute())
编辑:不确定您将获得一个数组还是一个ArrayCollection,请检查count功能是否对ArrayCollection有效。您离目标太远了。查询没有计数函数,这是querybuilder函数。您可以编写原始sql,但最好学习querybuilder:。记住,条令的目的是给你一个实体列表,你可以把它传递给树枝去处理 另外,你的小树枝也没什么用处,请参阅我所包含的非常粗糙的小树枝中的基本用法。使用
{{entity.method}
或{{{entity.field}}
获取每个实体的各种显示属性。用一根细枝在它们之间循环。请在您的上下文中查看我的示例,然后阅读细枝文档以填补空白
控制器
public function searchAction(Request $request) {
if ($request->getMethod() == 'GET') {
$title = $request->get('Search_term');
$em = $this->getDoctrine()->getManager();
$qb = $em->getRepository('YCRYcrBundle:Adverts')
->createQueryBuilder('a');
$searches= explode(' ', $title);
foreach ($searches as $sk => $sv) {
$cqb[]=$qb->expr()->like("CONCAT($sv, '')", "'%$sv%'");
}
$qb->andWhere(call_user_func_array(array($qb->expr(),"orx"),$cqb));
$adverts = $qb->getResult();
}
return $this->render('YCRYcrBundle:Search:search.html.twig'
, array('adverts' => $adverts));
}
还有一把树枝锉
{# Your other stuff #}
{% for advert in adverts %}
{{advert.getId}}
{# call the various method and fields for your display#}
{% endfor %}
当它“不工作”时会发生什么?您将获得
$title
并将其发送回。您的结果在$repository
中,因此我认为$repository
是您应该在呈现
状态中返回的内容抱歉,如果我不清楚“不工作”,我会得到以下错误:>FatalErrorException:错误:调用未定义的方法条令\ORM\Query::count()在C:\wamp\www\201308\src\YCR\YcrBundle\Controller\SearchController.php第28行中。我尝试了它,但我认为我的mysql语法不正确,因为它给了我以下错误:“[语法错误]第0行第7列:错误:应为IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression |”(“子选择”)“| CaseExpression,Get'*.”我认为它无法识别Select*
。通过将其替换为Select a FROM YCRYcrBundle:Adverts WHERE
,解决了该问题,但现在它告诉我,我的通配符%
:[syntax Error]第0行附近的语法错误,第59列:错误:应为StateFieldPathExpression | string | InputParameter |函数ReturningString | AggregateExpression,获取“%”。对于通配符,你怎么看ibn DQL?我肯定不会。谢谢你注意到打字错误。
public function searchAction(Request $request) {
if ($request->getMethod() == 'GET') {
$title = $request->get('Search_term');
$em = $this->getDoctrine()->getManager();
$qb = $em->getRepository('YCRYcrBundle:Adverts')
->createQueryBuilder('a');
$searches= explode(' ', $title);
foreach ($searches as $sk => $sv) {
$cqb[]=$qb->expr()->like("CONCAT($sv, '')", "'%$sv%'");
}
$qb->andWhere(call_user_func_array(array($qb->expr(),"orx"),$cqb));
$adverts = $qb->getResult();
}
return $this->render('YCRYcrBundle:Search:search.html.twig'
, array('adverts' => $adverts));
}
{# Your other stuff #}
{% for advert in adverts %}
{{advert.getId}}
{# call the various method and fields for your display#}
{% endfor %}