Given that I Fetch all my entities up front
// Get All Users
$qb = $em->createQueryBuilder();
$qb->select("u")-&开发者_开发技巧gt;from->("User u");
$q = $qb->getQuery();
// Doesn't this store any entities returned in the repository?
$r = $q->getResult();
If I then interrogate my repository, I would expect it not to have to go back to the database to resolve my query - but it does. Why is this?
// Get a particular user
$user = $em->getRepository('\User')->findByName('JoeBloggs');
// Is generating another query same as above but with 'where' clause...
Is there any way that I can use one DQL query to populate my repo and then continue to interrogate the repo for entities without having to hit the DB every time?
As an aside is there any way to initially populate the repo using more than one DQL query?
Repositories in Doctrine are a façade for object storage, when in reality it is just an abstraction layer for accessing the database. It does not "hold on to" any of the objects that are returned from queries.
Doctrine keeps track of objects that have been loaded in the UnitOfWork, but the only index it keeps is on the identifier. So if you are using the find() function for an object that is already loaded, it will not hit the database, but any other query will.
While there is no built-in way to do what you are asking, Doctrine does allow you to create custom repository classes in which you could implement this yourself. Be careful though, as you may find that some of these lookups in PHP will be slower than performing a SQL query.
精彩评论