0

In a Symfony 2 application I need a model class (without persistence in database) with a business logic features that involves to other entities (with persistence).

In my conceptual model there are "Rooms", "Connections" and "Algorithms". The Rooms and the Connections are Symfony entities based on doctrine. The Algorithms should be model classes or entities without persistence in database.

The business logic is the follow: - There are different kinds of Algorithms - One Algorithm makes relations between Rooms and Connections in function different criterias. This implies to retrieve Rooms or Connections from database, apply the logic, modify or create new entities to database.

The first approach I've made is an Algorithm Factory as a Symfony Service to provide a object to create an Algorithm base on a type parameter.

The controller invokes this service and call the algorithm's function to apply the logic.

The conceptual problem I've is the following: I don't know which type of Symfony element should be the Algorithm class. If it's an Entity it can't get Rooms or Connections from database with Doctrine Manager, isn't it? Should be it a Controller as a Service in terms of Symfony?

I've my doubts in how to represent this in Symfony architecture.

Thank you

1 Answers1

0

It doesn't have to be controller or entity, you can have it as a normal PHP class and call it and work with it within the other, Symfony-specific elements.

Where to put it is also a matter of preference.

If you need to access Entities, you can define this new class as service and inject other services as arguments.

Example: In your config.yml

app.algorithms_factory:
    class: AppBundle\Utils\AlgorithmsFactory
    arguments: [@doctrine.orm.entity_manager]

In your controller:

class RoomController extends Controller
{
    public function editRoomAction($roomId, Request $request = null)
    {
        $algorithmFactory = $this->get('app.algorithms_factory');
        ....
    }
}
pmihova
  • 68
  • 6