Personally, I found that the repository classes are a little inflating things in a small and medium sized application. Not sure what your approach is, but most of everything I read (and what I posted in a recent Doctrine 2 application) should have had a "service" level that managed entities. This is b / c in D2, the implementation of save / delete, etc. In essence, it undermines the purpose of the system, which is to facilitate the knowledge of persistence from entities and treat them as objects Plain Old Php (TM);)
The thing that seems strange to me is to pass in the primary key identifier and user id to retrieve the user. It seems to me that the pk of the User table will be the user ID, or at least if the user ID is not pk (not sure why this would be), you should be able to get entries using only pk. Here is a method to retrieve a User object on my system
public function fetch($iId) { return $this->_oEm->find('AwesomeApp\Entity\User', $iId); }
The current user function you are looking for must be associated with a session in your application. In zf, I created a session handler that stores the user doctrine object in the session store, and then when the session is read, I reattach the User object to the entity manager. You probably want to do something like this in sf, then calling getCurrentUser will return the same User object as pulling it out of the database. Saving the User object in the session prevents you from having to return to the database for it each time the page loads, for example, if you just saved the user ID in the session.
At the end of the day, you were "supposed" to put complex selection queries in the repository, but this obviously remains at the discretion of the user when it comes to best practices. In this case, when you only have pk, I would say that it makes no sense to write a repository class.
source share