Symfony2 - 网站搜索功能

时间:2014-06-23 08:21:22

标签: symfony search

我的Web应用程序中有一个实体的工作搜索功能。

但我想将此扩展到我网站上的多个实体(不同的标题),每个实体包含不同的帖子。

实体不相关,我想使用一个通用搜索一次查询所有实体,这是否可能?如果不是我的选择?

搜索服务

public function indexAction(Request $request)
{
    $results = null;
    $query = $request->query->get('q');

    if (!empty($query)) {
        $em = $this->getDoctrine()->getEntityManager();

        $results = $em->createQueryBuilder()
            ->from('AcmeDemoBundle:Article', 'a')
            ->select('a')
            ->where('a.title LIKE :search')
            ->setParameter(':search', "%${query}%")
            ->getQuery()
            ->getResult();
    }

    return array(
        'query' => $query,
        'results' => $results,
    );
}

searchAction

public function searchAction()
{
    // Search code: calling from the service Search
    $query = $this->get('search');
    $posts = $query->search();

    return array(
        'query'        => $query,
        'posts'      => $posts['results'],
    );
}

search.html.twig

{% block body %}
{{ parent() }}

        <div id="main">

            <div class="article-wrapper">

                {% for post in posts %}
                    {% autoescape false %}
                    {{ include('AcmeDemoBundle:Partial:_post.html.twig', { post: post }) }}
                    {% endautoescape %}
                {% else %}
                    <p>No results found</p>
                {% endfor %}

            </div>

        </div><!-- end Main content -->

        {% block sidebar %}
            {% render controller('AcmeDemoBundle:Page:sidebar') %}
        {% endblock %}

        {% block footer %}
            {% render controller('AcmeDemoBundle:Page:footer') %}
        {% endblock %}
    </div>
</div>

{% endblock %}

_post.html.twig

<article id="" class="format-standard hentry has-thumbnail">

        <div class="entry-thumbnail">
            <img width="230" height="172" src="{{ asset(['images/', post.image]|join) }}" class="" alt=""/>
        </div>

        <header class="entry-header">
            <h1 class="entry-title"><a href="{{ path('acme_demo_page_show', { slug: post.slug }) }}" title="">{{ post.title }}</a></h1>

            <p class="by-line"> by <a class="author-link" href="" rel="author">{{ post.author }}</a> on {{ post.createdAt|date('l, F j, Y') }}</p>
        </header>
        <!-- .entry-header -->

        <div class="entry-content">
            {% autoescape false %}
            <p>{{ include(template_from_string(post.body|truncate(1000))) }}</p>
            {% endautoescape %}
        </div><!-- .entry-content -->
        <div class="clear"></div>

    </article>

2 个答案:

答案 0 :(得分:0)

我不知道它是否有帮助,但你可以做原始SQL。

$em = $this->getDoctrine()->getManager();
$sql = " 
SELECT *
FROM db1 d1 
LEFT JOIN db2 d2 ON d1.id = d2.id
WHERE  ...
";
$stmt = $em->getConnection()->prepare($sql);
$stmt->bindParam(':state_id', $state_id);
$stmt->execute();
$db = $stmt->fetchAll();

如果它们与任何ID无关,我会做两个查询并创建一个新数组:$result1$result2,就像这样。

$result_all = array();
foreach ($result1 as $v1) {
    foreach ($result2 as $v2) {
    //array_push or $result_all[] = $v2...
    }

希望它有所帮助。

答案 1 :(得分:0)

首先请尝试创建纯SQL 据我所知,这看起来应该是

"SELECT * FROM AcmeDemoBundle:Article a, AcmeDemoBundle:Post p WHERE a.title = :search or p:title = :search"

然后将其转换为查询构建器(不测试但应该工作)

 $results = $em->createQueryBuilder()
            ->from('AcmeDemoBundle:Article', 'a')
            ->from('AcmeDemoBundle:Post', 'p')
            ->select('a, p')
            ->orWhere('a.title LIKE :search')
            ->orWhere('p.title LIKE :search')
            ->setParameter(':search', "%${query}%")
            ->getQuery()
            ->getResult();