Reputation: 5414
In the (incredibly confusing) documentation for CakePHP 3.x it gives the following under the New ORM Upgrade Guide:
You can decorate queries with iterators and call methods without even touching the database. This is great when you have parts of your view cached and having the results taken from the database is not actually required:
// No queries made in this example!
$results = $articles->find()
->order(['title' => 'DESC'])
->formatResults(function (\Cake\Collection\CollectionInterface $results) {
return $results->extract('title');
});
Why would you have the code above at all, if "having the results taken from the database is not actually required"?
Upvotes: 3
Views: 96
Reputation: 60503
You have to read that in the context that it is embedded in, the whole section is about how "calling find
on a table will not return the results immediately, but will return a Query object", and in what situations that can be useful.
Query objects do not automatically generate and run SQL, they only do so when explicitly invoking specific methods like toArray()
, all()
, collection methods, etc, or when iterating the object.
The situation described there is when the part of your code where the query object is finally being executed, ie the actual SQL query is being constructed and run, has been cached (for example a view template), so that running the query again on subsequent requests would be pointless and would just produce unnecessary overhead, as you already have the cached results.
See also
Upvotes: 7