Symfony2 - 使用未定义的getDoctrine()设置服务错误

时间:2014-04-02 04:28:52

标签: php symfony search service doctrine-orm

我正在尝试将搜索功能设置为服务。我的代码如下所示,在提交搜索字时出现以下错误:

FatalErrorException:错误:在/var/www/html/Project/src/Acme/ProjectBundle/Services/Search.php第24行中调用未定义的方法Acme \ ProjectBundle \ Services \ Search :: getDoctrine()

我是否需要以某种方式注入教义?

config.yml

services:
    search:
    class: Acme\ProjectBundle\Services\Search
    arguments: ["@request_stack"]
    scope: request

IndexAction Controller

$query = $this->get('search');
$results = $query->search();
return $this->render('AcmeProjectBundle:Default:index.html.twig, array(
    'query'  => $query,
    'results => $results,
));

搜索服务

class Search
{
private $request;

public function __construct(RequestStack $requestStack)
{
    $this->request = $requestStack->getCurrentRequest();
}

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

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


        $results = $em->createQueryBuilder()
            ->from('AcmeProjectBundle:Blog', 'b')
            ->select('b')
            ->where('b.title LIKE :search')
            ->setParameter(':search', "%${query}%")
            ->getQuery()
            ->getResult();
    }
//        exit(\Doctrine\Common\Util\Debug::dump($em));
    return array(
        'query'   => $query,
        'results' => $results,
    );
}
}

index.html.twig

{% block search %}
<form action="{{ path('acme_project_search') }}" method="GET">
    <label><input type="search" name="q" value={{ app.request.query.get('q') }}></label>
    <input type="submit" value="Search">
</form>
<br>
{% endblock %}

{% for result in query %}

    <div class="container">
    <div class="row">
        <div class="col-sm-8 blog-main">
            <div class="blog-post">

                <h2 class="blog-post-title">{{ result.title }}</h2>
                <p class="blog-post-meta"><time datetime="{{ result.created|date('c') }}">{{ result.created|date('l, F j, Y') }}</time> by <a href="#">{{ result.author }}</a></p>

                <p>{{ result.blog|truncate(350, true) }}</p><br>

                <div class="tags">
                    <p><strong>Tags: </strong><span class="highlight">{{ result.tags }}</span></p>
                </div>

            </div><!-- /.blog-post -->
        </div>
    </div>
</div>
{% endfor %}

2 个答案:

答案 0 :(得分:1)

是的,你需要“手动”注入教义。您的服务定义应如下所示:

services:
    search:
        class: Acme\ProjectBundle\Services\Search
        arguments: ["@request_stack", @doctrine]
        scope: request

你的构造函数应该如下:

use Doctrine\Bundle\DoctrineBundle\Registry;
public function __construct(RequestStack $requestStack, Registry $doctrine)
{
    $this->request = $requestStack->getCurrentRequest();
    $this->doctrine = $doctrine;
}

然后您可以在search方法中使用它:

public function search()
{
    (...)
    $em = $this->doctrine->getManager()
}

注意:您尝试使用的$this->getDoctrine()方法是Symfony\Bundle\FrameworkBundle\Controller类的方法,您可能已经习惯了从控制器中使用它,但是您需要注入自己的服务doctrine作为依赖

答案 1 :(得分:1)

您需要让容器为您的服务注入EntityManager

services:
    search:
    class: Acme\ProjectBundle\Services\Search
    arguments: ["@request_stack","@doctrine.orm.entity_manager"]
    scope: request