Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching data through a custom repository in a Twig extension

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'));
    }
}
like image 529
mattyh88 Avatar asked Dec 09 '11 19:12

mattyh88


2 Answers

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

like image 85
Matt Avatar answered Oct 31 '22 05:10

Matt


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 ... 

}
like image 41
Rubén Fanjul Estrada Avatar answered Oct 31 '22 05:10

Rubén Fanjul Estrada