Ton Gok
Ton Gok

Reputation: 152

Symfony Twig global variables

I'm trying to call some data in my root twig template and used Twig global variables to achieve that.

When I call the service i get this error:

Type error: Argument 1 passed to AwarenessBundle\Service\AwarenessService::getLatestNews() must implement interface Doctrine\ORM\EntityManagerInterface, none given, called in /var/www/html/myisms/vendor/twig/twig/lib/Twig/Template.php on line 675

Twig code:

{{ news_service.getlatestNews() }}

services.yml:

news.latest:
    class: AwarenessBundle\Service\AwarenessService
    arguments: [ '@doctrine.orm.entity_manager' ]

AwarenessService

class AwarenessService 
{
    public function getLatestNews(EntityManagerInterface $em)
    {
        return $em->getRepository("AwarenessBundle:Feed")
            ->findBy(array(), array('id' => 'DESC', 10));
    }
}

I'm not sure where my problem is but I have to make my service global and I don't know how to do that. Any help would be appreciated.

Upvotes: 0

Views: 633

Answers (1)

Petro Popelyshko
Petro Popelyshko

Reputation: 1379

Pass EntityManagerInterface to your service constructor(instead of getLatestNews method), like this:

   class AwarenessService 
    {
        /**
         * @var EntityManagerInterface
         */
        protected $em;

        public function __construct(EntityManagerInterface $em){
             $this->em = $em;
        }

        public function getLatestNews() {

            return $this->em->getRepository("AwarenessBundle:Feed")
                ->findBy(array(), array('id' => 'DESC', 10));

        }

}

Service.yml:

news.latest:
    class: AwarenessBundle\Service\AwarenessService
    //arguments for constructor!
    arguments: [ '@doctrine.orm.entity_manager' ]

Upvotes: 2

Related Questions