Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 389 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fetching data through a custom repository in a Twig extension

#1
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'));
}
}
Reply

#2
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](

[To see links please register here]

)

Hope this helps.

Regards,<br/>
Matt
Reply

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

}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through