peace_love
peace_love

Reputation: 6471

How can I use doctrine method inside my service (Symfony 4)?

I created my first own service in Symfony :

// src/Service/PagesGenerator.php 

namespace App\Service;

class PagesGenerator
{
    public function getPages()
    {

      $page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);

        $messages = [
            'You did it! You updated the system! Amazing!',
            'That was one of the coolest updates I\'ve seen all day!',
            'Great work! Keep going!',
        ];

        $index = array_rand($messages);

        return $messages[$index];
    }
}

But I get the error message:

Attempted to call an undefined method named "getDoctrine" of class "App\Service\PagesGenerator".

I tried then to add in my services.yaml:

PagesGenerator:
    class: %PagesGenerator.class%
    arguments:
      - "@doctrine.orm.entity_manager"

But then I get the error message:

The file "/Users/work/project/config/services.yaml" does not contain valid YAML in /Users/work/project/config/services.yaml (which is loaded in resource "/Users/work/project/config/services.yaml").

Upvotes: 1

Views: 10448

Answers (3)

thafirstone
thafirstone

Reputation: 92

With Symfony 4 and the new autowiring you can easily inject certain number of class

To find out, which classes/interface you can use for autowiring, use this command:

bin/console   debug:autowiring 

We are going to use this one :

Doctrine\ORM\EntityManagerInterface (doctrine.orm.default_entity_manager)

So let's make it, add this just before getPages function

/**
 * @var EntityManagerInterface
 */
private $em;

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

Then you can use it like this:

  $page = $this->em->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);

Hope it helps !

Upvotes: 3

Noman
Noman

Reputation: 4116

make sure you use proper indent using "spaces" for YAML.

A YAML file use spaces as indentation, you can use 2 or 4 spaces for indentation, but no tab read more about this

Before symfony 3.3

for example we have service sms_manager in AppBundle/FrontEndBundle/Services

services:
    AppBundle.sms_manager:
        class: AppBundle\FrontEndBundle\Services\SmsManager
        arguments: [ '@service_container' ,'@doctrine.orm.entity_manager' ]

then your service can receive your arguments in constructor

<?php

namespace AppBundle\FrontEndBundle\Services;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class SmsManager {
    private $container;
    private $DM;

    public function __construct( Container $container, \Doctrine\ORM\EntityManager $DM ) 
    {
        $this->container = $container;
        $this->DM        = $DM;
    }

    /**
    * @return \Doctrine\ORM\EntityManager
    */

    public function getDoctrine() {
      return $this->DM;
    }

}

With Symfony 3.3 or more,

Is there a way to inject EntityManager into a service

use Doctrine\ORM\EntityManagerInterface

class PagesGenerator
{
    private $em;

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

    // ...
}

Upvotes: 1

GasKa
GasKa

Reputation: 663

So, in comments I was saying that is better to let Symfony doing his job and autowiring EntityManager. This is what you should do. Also, can you tell us what Symfony version are you using and if autowiring is enabled (check services.yaml for that)?

<?php

namespace App\Service;

use Doctrine\ORM\EntityManagerInterface;

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

    public function getPages()
    {

      $page = $this->em->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);

        $messages = [
            'You did it! You updated the system! Amazing!',
            'That was one of the coolest updates I\'ve seen all day!',
            'Great work! Keep going!',
        ];

        $index = array_rand($messages);

        return $messages[$index];
    }
}

Upvotes: 11

Related Questions