0

I am using an entity class which extends ServiceEntityRepository like this:

class Sms extends ServiceEntityRepository
{ 
   public function __construct(ManagerRegistry $registry)
   {
       parent::__construct($registry, Sms::class);
   }
... 
}

so when I need to persist an instance of the entity class in my controller file I need to pass the ManagerRegistry as the argument for my Entity class but I couldn't find a way to access the ManagerRegistry in my controller.

can anyone help?

24
  • The service might be private so you can't get it from the container with $this->container->get(ManagerRegistry::class). Just inject the ManagerRegistry into your controller or override the service definition and make it public. Commented Aug 22, 2019 at 13:50
  • 1
    It would be very strange to have a Doctrine entity extend a repository. No active record stuff in Doctrine. So your approach is fundamentally wrong. And as far as your question goes, the code you posted shows how to inject the registry into a class. Not that you need to anyways. Commented Aug 22, 2019 at 13:50
  • 1
    I didn't quite follow a) why your entity is extending ServiceEntityRepository (which is meant to be extended by repository classes instead of entities) and b) why you need ManagerRegistry in your controller to save an entity instance? Commented Aug 22, 2019 at 13:52
  • I extend that because I was seeking "query Builder" in my entity class. Commented Aug 22, 2019 at 13:56
  • @Cerad and @ejuhjav - Guys relax ServiceEntityRepository is just an optional EntityRepository base class with a simplified constructor (for autowiring).. I don't see any bad practice extending it - as it's meant to be. The class-name Sms might indicate a "bad" approach but it doesn't necessarily from the code in the question. Changing the class-name to SmsRepository might be more obvious, agreed :) Commented Aug 22, 2019 at 13:56

1 Answer 1

1

The problem was that ServiceEntityRepository should be extended in a repository class, not an entity class. as it is mentioned here, there is no good description for auto-generating repositories from an existing database. This is my entity class with its annotations:

/**
* Sms
*
* @ORM\Table(name="sms")
* @ORM\Entity(repositoryClass="App\Repository\SMSRepository")
 */
class Sms
{ ... }

this line is very important: @ORM\Entity(repositoryClass="App\Repository\SMSRepository")

another important thing is to removing Entity from excluding in the services.YAML file.

if you set a name for the repository of your entity class by annotation, by running this command you will have your repository generated:

php bin\console make:entity --regenerate

and you can simply write you complex queries in the repository file which is generated by the aforementioned command.

for calling methods of your repository class you can use this in your controller files:

$this->getDoctrine()->getRepository(EntityFile::class)->youFunctionNameInRepositoryFile()

be careful about the argument of the getRepository which is the Entity file, not the repository file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.