Posts Tagged ‘ Model ’

CakePHP and in-memory query caching

CakePHP models by default cache in-memory all database queries you make in each request, this reduces the database querying overhead when redundant queries are made, assuming that nothing changed. But sometimes there’s a need to implement complex methods in your models to perform complex and crazy actions with your data, and sometimes when you do that you need to make the same query many times, for example, when you’re checking something inside any loop, and when you do that, if your model is caching queries, you’ll get always the same result, that will bring you for sure an unexpected behavior. So when you do that, you just need to disable query caching by setting the cacheQueries variable to false in your model before the database query you don’t want to cache, and enabling it after, if needed. It’ll make you model to generate a real fetch of data in the server. I believe that some of you are thinking which it can be avoided if you do any kind of data handling in your logic, avoiding the model to do repeated fetches to the database, and you’re right, but each situation is unique, and this need is not very usual. I just want you to be aware about the models query cache existence :)