Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Entity and Model in Symfony2 / Doctrine

I am going through the FOSUserBundle.

And I wanted to undertand what is the difference between Model/User.php and Entity/User.php.

Can't the Model and Entity be same?

like image 749
Mirage Avatar asked Jul 12 '12 01:07

Mirage


People also ask

What is the difference between model and entity?

Entity: An entity represents a single instance of your domain object saved into the database as a record. It has some attributes that we represent as columns in our tables. Model: A model typically represents a real world object that is related to the problem or domain space.

What is the model in Symfony?

Symfony's default model component is based on an object/relational mapping layer. Symfony comes bundles with the two most popular PHP ORMs: Propel and Doctrine. In a symfony application, you access data stored in a database and modify it through objects; you never address the database explicitly.

What is entity Symfony?

Well, entity is a type of object that is used to hold data. Each instance of entity holds exactly one row of targeted database table. As for the directories, Symfony2 has some expectations where to find classes - that goes for entities as well.

What is repository in Symfony?

A repository is a way to retrieve entities, so put on repositories any method you need to get them, such as getUserByEmail or whatever.


1 Answers

Here's the best summary on the roles and terminology of Doctrine 2.

The Entity is used by the UnitOfWork pattern in Doctrine 2.0 ORM (and in Hibernate in the Java world) and is also an object representation of a thing in the real world. It has the same attributes and methods as the record would have but it does not know about its persistence. It’s basically a POPO (plain old PHP object). This allows these classes and objects to be very lightweight.

The Model is a conceptual object representation of a thing. This term could be used for all the above. A record is a model just as an entity is or a document. The term describes an object representation of a thing.

So if you want to create a bundle with some models in it and you want your bundle to be independent of any persistence layer you would create model classes and interfaces that define the thing.

You would define and / or implement everything in there that is not specific to any persistence implementation and you would use a related entity or document class via delegation to handle the specific stuff.

This is done for example in the FOSUserBundle which defines a User model. This User model is used throughout the bundle but doesn’t have a complete implementation to access its own data (I didn’t look but I think it could be an interface only). The final implementation is done in an entity class and another in a document class so no matter whether you use MySQL or MongoDB you can still work with the same User model. You can even switch from one to the other without changing your code as it always uses the model which is independent from the entity or document.

like image 135
Travis Avatar answered Oct 14 '22 13:10

Travis