通过 Twig 扩展中的自定义存储库获取数据 [英] Fetching data through a custom repository in a Twig extension

查看:19
本文介绍了通过 Twig 扩展中的自定义存储库获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 symfony 2 网络应用程序的每一页上显示新的通知.我被建议为此使用 Twig Extension.我在该扩展中创建了一个函数 getFriendRequests,但我不知道通过我的自定义存储库在树枝扩展中获取数据是否是一个好习惯:现在它给了我错误,它找不到 getDoctrine 方法.

I'd like to display new notifications on every page of my symfony 2 webapplication. I was advised to use a Twig Extension for this. I've created a function getFriendRequests in that extension, but I don't know if it's good practice to get data through my custom repository in the twig extension: Right now it's giving me the error, that it can't find the getDoctrine method.

<?php

namespace TennisconnectDashboardBundleExtension;

class NotificationTwigExtension extends Twig_Extension
{
    public function getFriendRequests($user)
    {
        $users = $this->getDoctrine()
            ->getRepository('TennisconnectUserBundle:User')
            ->getFriendRequests();
        return count($users);
    }

    public function getName()
    {
        return 'notification';
    }

    public function getFunctions()
    {
        return array(
            'getFriendRequests' => new Twig_Function_Method($this, 'getFriendRequests'));
    }
}

推荐答案

我认为直接从 twig 扩展中获取数据并不是什么坏事.毕竟,如果你不在这里做,你将需要先获取这些记录,然后再将它们传递给扩展程序以进行显示.

I don't think it is so bad to fetch your data directly from your twig extension. After all, if you don't do it here, you will need to fetch those records before and then pass them to the extension for display anyway.

重要的一点是像你已经在做的那样在存储库中做 DQL/SQL 的事情.将数据库语句与项目的其他部分分开很重要.

The important point is to do the DQL/SQL stuff in the repository like you are already doing. This is important to separate database statements from other part of your project.

您遇到的问题是该类中不存在方法 getDoctrine.据我了解,您从扩展 FrameworkBundle 基本控制器的控制器中获取了这段代码.FrameworkBundle 的基本控制器定义了这个方法.

The problem you having is that the method getDoctrine does not exist in this class. From what I understand, you took this code from a controller which extends the FrameworkBundle base controller. The base controller of the FrameworkBundle defines this method.

要解决这个问题,您必须将正确的服务注入到您的扩展程序中.这是基于依赖注入容器.你当然为你的树枝扩展定义了一个服务,像这样的定义:

To overcome this problem, you will have to inject the correct service into your extension. This is based on the dependency injection container. You certainly defined a service for your twig extension, something like this definition:

services:
  acme.twig.extension.notification:
    class: AcmeWebsiteBundleTwigExtensionNotificationExtension
    tags:
      - { name: twig.extension }

现在的诀窍是像这样注入你需要的依赖项:

The trick is now to inject the dependencies you need like this:

services:
  acme.twig.extension.notification:
    class: AcmeWebsiteBundleTwigExtensionNotificationExtension
    arguments:
      doctrine: "@doctrine"
    tags:
      - { name: twig.extension }

然后,在你的扩展中,你定义了一个接收信条依赖的构造函数:

And then, in you extension, you define a constructor that receives the doctrine dependency:

    use SymfonyBridgeDoctrineRegistryInterface;

    class NotificationTwigExtension extends Twig_Extension
    {
        protected $doctrine;

        public function __construct(RegistryInterface $doctrine)
        {
            $this->doctrine = $doctrine;
        }

        // Now you can do $this->doctrine->getRepository('TennisconnectUserBundle:User')

        // Rest of twig extension
    }

这就是依赖注入的概念.你可以看到我前段时间回答的关于访问控制器外的服务的另一个问题:这里

This is the concept of dependency injection. You can see another question I answered sometime ago about accessing services outside controller: here

希望这会有所帮助.

问候,
马特

这篇关于通过 Twig 扩展中的自定义存储库获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆