Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony, entity vs repository

Tags:

symfony

I'm new to Symfony, I come from a MVC framework where there were just one Model class, but here in Symfony we have 2, as far as I've learnt (Entity and Repository)

So, I'm not sure when a method should be put inside the Entity class and when it should be inside the Repository...

for example:

$user->logLogin()

which logs the user login (timestamp, ip, country, user agent ecc.) to a log_logins table, and it's called from a login event listener, where should it go? (I'm not going to create a UserLogins Entity since there is no need to manipulate the data on that table, it's just some read-only information)

like image 425
Carlo Giovanni Avatar asked Jun 08 '16 14:06

Carlo Giovanni


People also ask

What is the difference between entity and repository?

One key point to note is that an Entity is what gets stored in a database. A repository is what interacts with a database (there's a difference). As long as we need only simple operations (such as CRUD), we need not even write the queries for these, in case we're using JPA (Java Persistence API's).

What is an entity in 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 a repository Doctrine?

A repository mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. In Doctrine, a repository is a class that concentrates code responsible for querying and filtering your documents.

How should be the process to add a new entity to the app in Symfony?

With the doctrine:database:create command we create a new database from the provided URL. With the make entity command, we create a new entity called City . The command creates two files: src/Entity/City. php and src/Repository/CityRepository.


1 Answers

It might not be immediately evident, but in Symfony you do things with services. The login method does not belong either in the entity or the repository.

The general idea is as follows:

  • Entity: a data structure. Almost never has a meaningful method that is not an accessor.
  • Repository: The only part that interacts with a DB. You use the default ones in most cases but do need one if you have some special queries. Business logic should not be here either.
  • Services: where the business logic lives. Have a lot of them. Make them use other services and repositories

A typical controller invokes a few services, usually without touching repositories (or Doctrine) directly.

This structure is not apparent in many examples that cut out some of these layers in the interest of being as simple and brief as possible.

NOTE: Symfony has a rich login system which is already connected to several of its components and that you might use without without reimplementing a new one. I'd reccomend to have a look.

like image 104
Rad80 Avatar answered Oct 12 '22 03:10

Rad80