1

我想创建一个 searchController,它在数据库中搜索用户在文本框中输入的任何关键字。我看过这篇文章这篇文章以及更多。

这是我到目前为止所拥有的:

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: Error: Call to undefined method Doctrine\ORM\Query::count() in C:\wamp\www\201308\src\YCR\YcrBundle\Controller\SearchController.php 第 28 行,不知道有什么问题我是 Symfony 和学说的新手。

这就是我的 search.html.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
4

2 回答 2

2

你离题太远了。Query 没有计数功能,这是一个 querybuilder 功能。您可以编写原始 sql,但最好学习 querybuilder :http ://docs.doctrine-project.org/en/latest/reference/query-builder.html 。请记住,学说旨在为您提供一个实体列表,您可以将其传递给树枝以进行处理。

此外,你的树枝不会做太多,请参阅我所包含的非常粗糙的树枝中的使用基础知识。使用{{entity.method}}or{{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 %}
于 2013-04-30T01:54:56.367 回答
0

您需要执行查询(使用 execute ),否则您将不会得到一个数组。并且您应该使用查询生成器而不是 DQL 字符串连接。阅读有关基本学说用法的学说文档,实体名称空间似乎也不正确。

我引用你的话:

  $query = $em->createQuery($query);
            $numRow = $query->count();

至少应该是:

  $query = $em->createQuery($query);
            $numRow = count($query->execute())

编辑:不确定你会得到一个数组还是 ArrayCollection,检查计数函数是否适用于 ArrayCollection。

于 2013-04-24T19:11:15.220 回答