我是symfony2和doctrine的新手,我想创建一个searchController,它在数据库中搜索用户输入文本框的任何关键字。我查看了this article和this等等......
这是我到目前为止所做的:
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()));
}
此代码中缺少什么使其工作,或者出现了什么问题?
修改
抱歉,我不清楚。我收到以下错误:FatalErrorException:错误:在C:\ wamp \ www \ 201308 \ src \ YCR \ YcrBundle \ Controller \ SearchController.php第28行调用未定义的方法Doctrine \ ORM \ Query :: count() 因为我是Symfony和学说的新手,所以不知道出了什么问题。
这就是我在我的search.html.twig中所拥有的内容(我也很喜欢twig,所以请光临我):
{% extends "YCRYcrBundle::layout.html.twig" %}
{% block content %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>YCR Job Search</title>
<link href="stylesheets/style.css" rel="stylesheet" type="text/css" />
<link href="stylesheets/colour.css" rel="stylesheet" type="text/css" />
</head>
<body onload="b = setInterval('clear()', 0);">
<div class="topDiv">
<div style="float: left;">
<img src="/201308/View/images/ycr.jpg" alt="ycr"></div>
<br/>
<!--<H2>Search for a Job</H2>-->
<div class="searchform">
<form id="formsearch" name="p" method="get" action="index.php">
<span>
<input name="Search_term" class="editbox_search" id="editbox_search" maxlength="80" value="<?php $this; ?>" type="text" />
</span>
<input type="image" name="button_search" src="images/search.gif" class="button_search" alt="" />
</form>
<br/>
<div id="search_results">
</div>
</div>
<!-- </div>-->
<script type="text/javascript" src="http://code.jquery.com/jquery- 1.7.2.min.js"></script><!--javascript jquery library-->
<script type="text/javascript" src="../View/scripts/Script.js"></script>
{% endblock %}
当用户搜索例如程序员时,显示如下:
-Programmer
-a summary of the description of what a programmer entails
-a button/link that reads: read more, that takes me to a page with full description
有关如何使此搜索功能正常工作的任何建议将不胜感激。
答案 0 :(得分:2)
你离我很远。 Query没有count函数,这是一个querybuilder函数。您可以编写原始sql,但更好地学习querybuilder:http://docs.doctrine-project.org/en/latest/reference/query-builder.html。请记住,doctrine旨在为您提供一个实体列表,您将这些实体传递给树枝进行处理。
此外,你的枝条不会做太多,请参阅我所包含的非常粗糙的树枝中的使用基础知识。使用{{entity.method}}
或{{entity.field}}
获取每个实体的各种显示属性。使用树枝foreach循环遍历它们。在您的上下文中查看我的示例,然后阅读twig文档以填补空白。 http://twig.sensiolabs.org/documentation
控制器
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 %}
答案 1 :(得分:0)
您需要执行查询(使用execute),否则您将无法获得数组。并且您应该使用查询构建器而不是DQL字符串连接。阅读关于基本学说用法的学说文档,实体命名空间似乎也是不正确的。
我引用你:
$query = $em->createQuery($query);
$numRow = $query->count();
至少应该是:
$query = $em->createQuery($query);
$numRow = count($query->execute())
编辑:不确定您是获取数组还是ArrayCollection,请检查计数函数是否适用于ArrayCollection。