Carlo
Carlo

Reputation: 1194

symfony2 - Creating services with variable name

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.

Upvotes: 0

Views: 112

Answers (1)

Wouter J
Wouter J

Reputation: 41934

Some options:

Upvotes: 1

Related Questions