【问题标题】:Fetching data through a custom repository in a Twig extension通过 Twig 扩展中的自定义存储库获取数据
【发布时间】:2011-12-09 19:10:20
【问题描述】:

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

<?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'));
    }
}

【问题讨论】:

标签: symfony doctrine-orm twig


【解决方案1】:

我认为直接从 twig 扩展程序中获取数据并不是那么糟糕。毕竟,如果您不在这里执行此操作,则需要先获取这些记录,然后将它们传递给扩展程序以进行显示。

重要的一点是像您已经在做的那样在存储库中执行 DQL/SQL 内容。这对于将数据库语句与项目的其他部分分开很重要。

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

要解决这个问题,您必须将正确的服务注入到您的扩展程序中。这是基于依赖注入容器的。您当然为您的 twig 扩展定义了一个服务,类似于以下定义:

services:
  acme.twig.extension.notification:
    class: Acme\WebsiteBundle\Twig\Extension\NotificationExtension
    tags:
      - { name: twig.extension }

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

services:
  acme.twig.extension.notification:
    class: Acme\WebsiteBundle\Twig\Extension\NotificationExtension
    arguments:
      doctrine: "@doctrine"
    tags:
      - { name: twig.extension }

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

    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
    }

这就是依赖注入的概念。您可以看到我之前回答的另一个关于访问控制器外部服务的问题:here

希望这会有所帮助。

问候,
马特

【讨论】:

  • 哈,在答案上领先我几秒钟。打得好。
  • 呵呵,时机怎么样 :) 有时我也会在发布答案之前被殴打。
  • 感谢答案,但我收到此错误:第 18 行的“::base.html.twig”中未启用“通知”扩展。
  • 解决了..我复制了一点。必须将构造函数()编辑为构造(),并且标签前有几个或很多空格。我得到了输入提示界面而不是实际类的建议。我应该在 config.yml 中添加什么参数,因为现在构造函数正在使用 Registry 的实例。
  • 解决了 :) 我必须在我的 twig 扩展中为 Registry 接口添加一个 use 语句
【解决方案2】:

相同但使用 mongo:

在 config.yml 中

services:
    user.twig.extension:
        class: MiProject\CoreBundle\Twig\Extension\MiFileExtension
        arguments:
          doctrine: "@doctrine.odm.mongodb.document_manager"
        tags:
          -  { name: twig.extension }

在你的 Twig\Extensions\MiFile.php 中

<?php

namespace MiProject\CoreBundle\Twig\Extension;

use Symfony\Component\HttpKernel\KernelInterface;

class MiFileExtension extends \Twig_Extension
{
    protected $doctrine;

    public function __construct( $doctrine){
        $this->doctrine = $doctrine;
    }
    public function getTransactionsAmount($user_id){
    return $results = $this->doctrine
    ->createQueryBuilder('MiProjectCoreBundle:Transaction')             
    ->hydrate(false)
    ->getQuery()            
    ->count();
    }

    Rest of mi code ... 

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多