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)
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).
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With