find()/findFirst() without hitting database?


I'm wondering if there's a way to retrieve a set of models, then search that set without hitting the database.

For example, I have a Request model with a one-to-many relationship with a File model, called "Files". For my action, I need to both have the full list of all File models associated, but I also want to be able to search that list to see if any of them have a particular filename.

If I call getFiles() multiple times, once with my condition and once without, the database gets hit twice - less than ideal. I know I can use getFiles()->filter(), and it looks like that's the way I'll go, but I was simply wondering if there was another, more appropriate way to accomplish what I need.



You can implement a cache for database queries in your application:

I'm not sure caching will work. Reading that page, it looks like the caching solutions will cache a particular resultset. So if in the first place I want to retrieve all "Files", that's one resultset. Then, if I want to find all "Files" with the filename "test.txt", that's a separate resultset. What I want to be able to do is hit the database once to get all "Files", then when I want all "Files" with the filename "test.txt", I iterate through and filter down the resultset I have already retrieved and have in memory, rather than hitting the database again.