通过Twig扩展中的自定义存储库获取数据 [英] Fetching data through a custom repository in a Twig extension
问题描述
我被建议使用Twig扩展。我在该扩展中创建了一个函数getFriendRequests,但是我不知道在twig扩展中获取数据是否是通过我的自定义存储库是很好的做法:现在它给我的错误,它找不到getDoctrine方法。
<?php
命名空间Tennisconnect\DashboardBundle\Extension;
class NotificationTwigExtension extends \Twig_Extension
{
public function getFriendRequests($ user)
{
$ users = $ this-> getDoctrine()
- > getRepository('TennisconnectUserBundle:User')
- > getFriendRequests();
返回计数($ users);
}
public function getName()
{
return'notification';
}
public function getFunctions()
{
return array(
'getFriendRequests'=> new \Twig_Function_Method($ this,'getFriendRequests ));
}
}
不要以为直接从您的枝条扩展中获取数据是如此糟糕。毕竟,如果你不这样做,你将需要先获取这些记录,然后将它们传递给扩展名来显示。
重要的一点是像您已经在做的那样在存储库中执行DQL / SQL。将数据库语句与项目的其他部分分开是很重要的。
您遇到的问题是方法 getDoctrine
这个类不存在从我所理解的,你从一个控制器中获取了这个代码,它扩展了 FrameworkBundle
基本控制器。 FrameworkBundle
的基本控制器定义了这种方法。
为了克服这个问题,你必须注入正确的服务到您的分机。这是基于依赖注入容器。您确实为您的枝条扩展定义了一个服务,就像这样定义:
服务:
acme.twig.extension .notification:
class:Acme\WebsiteBundle\Twig\Extension\NotificationExtension
标签:
- {name:twig.extension}
现在,诀窍是注入您需要的依赖项,如下所示:
服务:
acme.twig.extension.notification:
class:Acme\WebsiteBundle\Twig\Extension\NotificationExtension
arguments:
doctrine :@doctrine
标签:
- {name:twig.extension}
然后,在你的扩展名中,你定义一个接受原则依赖的构造函数:
使用Symfony\Bridge\ Doctrine\RegistryInterface;
class NotificationTwigExtension extends \Twig_Extension
{
protected $ doctrine;
public function __construct(RegistryInterface $ doctrine)
{
$ this-> doctrine = $ doctrine;
}
//现在你可以做$ this-> doctrine-> getRepository('TennisconnectUserBundle:User')
//其余的twig扩展
}
这是依赖注入的概念。您可以看到我之前有关访问控制器外的服务的另一个问题:此处
希望这有帮助。
问候,
Matt
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 Tennisconnect\DashboardBundle\Extension;
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'));
}
}
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.
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.
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: Acme\WebsiteBundle\Twig\Extension\NotificationExtension
tags:
- { name: twig.extension }
The trick is now to inject the dependencies you need like this:
services:
acme.twig.extension.notification:
class: Acme\WebsiteBundle\Twig\Extension\NotificationExtension
arguments:
doctrine: "@doctrine"
tags:
- { name: twig.extension }
And then, in you extension, you define a constructor that receives the doctrine dependency:
use Symfony\Bridge\Doctrine\RegistryInterface;
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
Hope this helps.
Regards,
Matt
这篇关于通过Twig扩展中的自定义存储库获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!