Fetching data through a custom repository in a Twig extension

后端 未结 2 619
忘掉有多难
忘掉有多难 2020-12-03 04:02

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 getFriendReques

相关标签:
2条回答
  • 2020-12-03 04:36

    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

    0 讨论(0)
  • 2020-12-03 04:36

    The same but with mongo:

    in config.yml

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

    and in your 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 ... 
    
    }
    
    0 讨论(0)
提交回复
热议问题