1

i read the post on injecting repository into a service under Symfony21. I am trying since two days to adapt my code and I turning around.

I need your help.

So what is the problem :

I am adapting the bundle "RatingStar" (http://www.tipocode.com/symfony-2/symfony2-star-rating-system/) in Symfony3 and MongoDB.

I want use my mongodb in place doctrine (mysql) and i am lost :(

There is a service :

  starbar.twig_extension: 
    class: PortailBundle\Twig\StarBarExtension
    arguments: [ "@doctrine", "@request_stack" ]
    tags: 
      - 
        name: twig.extension

Note : i try to substitue "@doctrine" by factory... and so on but i have some errors in my class in the arguments list.

There is a class like this :

class StarBarExtension extends \Twig_Extension
{
    protected $doctrine; 
    private $RequestStack;


    public function __construct(RegistryInterface $doctrine, RequestStack $RequestStack)
    {
        $this->doctrine = $doctrine;
        $this->requestStack = $RequestStack;
    }

    public function getFunctions() {
      return array(
           'starBar' => new \Twig_Function_Method($this, 'myStarBar'),
      );
    }

    public function myStarBar($numStar, $mediaId, $starWidth) {

    $cookies = $this->requestStack->getCurrentRequest()->cookies->get('LilmodLemaedNote'.$mediaId);

    $nbrPixelsInDiv = $numStar * $starWidth; // Calculate the DIV width in pixel

    **$query = $this->get('doctrine_mongodb')->getRepository('PortailBundle:Notes')->findOneBy(array('ARTICLE' =>$mediaId));**

I follow a lot of example but without success. I don't know what i do with the service and what i have to declare in the class to use my query.

Please try to help me.

Community
  • 1
  • 1
Laurent
  • 11
  • 2

2 Answers2

0

You have no $this->get('doctrine_mongodb') because you not injected container. But what you do in that line of code, you getting a service called doctrine_mongodb, and this is yours needed service, so in services.yml:

arguments: [ "@doctrine_mongodb", "@request_stack" ]

and in your StarBarExtension class:

$query = $this->doctrine->getRepository('PortailBundle:Notes')->findOneBy(array('ARTICLE' =>$mediaId));

Remove RegistryInterface from your constructor as well.

malcolm
  • 5,486
  • 26
  • 45
0

yes, you are right.

I change the service.yml like this :

starbar.twig_extension: 
    class: PortailBundle\Twig\StarBarExtension
    arguments: [ "@doctrine.odm.mongodb.document_manager", "@request_stack" ]
    tags: 
      - 
        name: twig.extension

and in the class StarBarExtension :

namespace PortailBundle\Twig;
use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Component\HttpFoundation\Cookie;

  class StarBarExtension extends \Twig_Extension
    {
      protected $doctrine;
      private $RequestStack;


 public function __construct(DocumentManager $doctrine, RequestStack $RequestStack)
{
    $this->doctrine_mongodb = $doctrine;
    $this->requestStack = $RequestStack;
}

public function getFunctions() {
  return array(
       'starBar' => new \Twig_Function_Method($this, 'myStarBar'),
  );
}

public function myStarBar($numStar, $mediaId, $starWidth) {

$cookies = $this->requestStack->getCurrentRequest()->cookies->get('LilmodLemaedNote'.$mediaId);

$query = $this->get('doctrine_mongodb')->getRepository('PortailBundle:Notes')->findOneBy(array('ARTICLE' =>$mediaId));

and the result is :

Attempted to call an undefined method named "get" of class "PortailBundle\Twig\StarBarExtension".
Did you mean to call e.g. "getFilters", "getFunctions", "getGlobals", "getName", "getNodeVisitors", "getOperators", "getTests" or "getTokenParsers"? 

I think, it is normal, but how to be able to launch the request ?

---------- i found a solution

I don't know if it is the good way but i have the request :

just change

$query = $this->get('doctrine_mongodb')->getRepository('PortailBundle:Notes')->findOneBy(array('ARTICLE' =>$mediaId));

into

$query = $this->doctrine_mongodb->getRepository('PortailBundle:Notes')->findOneBy(array('ARTICLE' =>$mediaId));

bye

Laurent
  • 11
  • 2