Should entities always be database-based in Symfony2?

I have a project where the database is hiding behind web services (many of them). In short, direct access to the database is not possible.

I use Symfony2, and I force myself to use the object every time I have an object that “stores data” (for example, user, car, room) and services (accessible from the container) / models when heavier logic is involved (for example , TransactionMaker, RoomBooker, CarDestroyer, etc.).

Entities, without any ORM descriptions, were chosen over arrays because the framework provides an extremely easy way to validate, create a form, and intellisense IDE.

Now it works fine, but some developers claim that the object should always reflect the table in the database. It's true?

+8
symfony
source share
1 answer

Entities only display the database table when they are configured for this, from the book she reads:

A class — often referred to as an “entity,” meaning a base class that contains data — is simple and helps you meet the business requirements associated with the needs of the products in your application. http://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class

It’s good practice to model your data in a more formal way than using simple arrays, which is why they exist.

Perhaps they are most often used to map data to a database, but this is not a requirement. They perform the task of creating data containers that model the information in your application in a way that makes sense. (i.e. model USER in USERS)

If you do not use the database to save your objects, you can use them to transfer data, create forms, use the verification service, security, etc. It might also be a good idea to create a service that allows you to access information about your web services from your Symfony application so that you can have something like:

$user = $this->get('some_persistance_service_you_write')->find($id,'user'); $user->setName('new value'); $err = $this->get('validator')->validate($user); //.... $this->get('some_persistance_service_you_write')->persist($user); 

This, of course, is off topic, but it is an example of how you can use objects without access to the database.

+8
source share

All Articles