In this chapter, we will cover:
Using models to fetch data is one of the most important aspects of any CakePHP application. As such, a good use of the find functions the framework provides can certainly guarantee the success of our application, and as importantly ensure that our code is readable and maintainable.
CakePHP provides the following basic find types:
all
: To find all records that match the given find options.count
: To count how many records match the given options.first
: To find the first record that matches the given find options.list
: To find all records that match the given find options, and formats them as a list, using the format provided.neighbors
: To find the previous and after records of a matching record, based on the value of a particular field.threaded
: To finds a set of results, and return them in a hierarchy, based on the value of a field named parent_id
.Mastering these types is as easy as understanding the available find options all types deal with. In this chapter, we have several recipes to make the most out of these options, and to resort to manual SQL based queries when the need arises.
CakePHP also lets us define our custom find types that will extend the three basic ones, allowing our code to be even more readable. The last recipes in this chapter show us how to create our own find type, with pagination support.
3.138.119.106