0

Let's say I have 50 classes which perform database operations via a DBAL connection. Now I want to make these classes as services with the DBAL connection injected when instantiated.

I call one of those services method with:

$results = $this->get('my_service')->someMethod();

with the service defined as:

services:
  my_service:
    class:  Acme\BundleName\Entity\ModelClass
    arguments:  [@database_connection]

is there any way to make that "my_service" variable?

Ideally, I would have one service, who accepts the name of the model class, and automatically instantiate and injects it with the DBAL connection. Without defining 50 services for 50 classes. The 50 classes only need the same injection, the DBAL connection.

1 Answer 1

1

Some options:

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

1 Comment

Thank you. I actually tried to use a service factory as documented in the symfony book, but, if I am not wrong, there are no variables, the name of the classes must still be defined one by one in the YAML file. Same for the Abstract parent class. Could you give me an example?

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.