Zend Framework, Architecture with Doctrine

82 Views Asked by At

im learning Zend Framework (3) ... I installed Doctrine because i do not want to write sql queries to learn all other stuff faster....

First Question:

So know i configured a factory that loads me the doctrine entity manager to my Controllers (with DI).

So its really simple to get my entities to my controller ... e.g in my Project Controller createAction i can easy get my user entities to show them in my project form (project <--> user many-to-many).

But know im struggling, would it be better to create repository classes and inject this to my controllers instead of the doctrine entity manager, so i can filtering etc all my entities?

Second question:

When i want to filter my projects (e.g by user) where should i do this ... in simpler slim projects i created Collection classes and injected them all of my entities and after that i called a filter method in my collection ... but the problem there is, i just loaded all entities from my database to the collection. In larger application i think there are to many entities loaded from the Database?

Third Question (Conclusion):

When i want to load data from the db to my Controllers whats best Practice here?

Load it from the entity manager ?

Load it from a Repository (the Repository load it from the entity manager)?

Load it from a Collection (the Collection loads data from the repository class and the repository loads data from the entity manager)?

I did not thought about Pagination @all... thats what i have to do next ... but there are many questions similar to my other questions. (I know there is a zend module for this .. but have no idea how this works.. have too learn this too)

Im thankful for every hint, meaning etc.

1

There are 1 best solutions below

1
On

Answers to this question are possibly all opinion based. I would say it is all about personal preference. There is no such thing as best practice here it all depends on what you will do in your controller. On top of that you can easily get a repository from your entity-manager if you need it:

$userRepository = $entityManager->getRepository('Application\Entity\User');

A more common might be to make a custom UserService (a wrapper class) around your repository/entity-manager that you populate with the custom methods you would like to use with your User resources.

I hope this helps you a bit...