Book Image

CakePHP 2 Application Cookbook

Book Image

CakePHP 2 Application Cookbook

Overview of this book

Table of Contents (20 chapters)
CakePHP 2 Application Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Custom finders


Instead of creating custom getter methods in your models, like we did in our previous recipe by running find() internally and returning an array of results, you can also use the built-in feature to define custom finder methods. These can modify the find options before the query is executed or even fix the returned results before they are used.

Custom finders provide a better interface to reuse common find operation parameters. Instead of working only with the result arrays (containing the database rows in array format), we'll be able to work with the find options first.

In this recipe, we'll create a custom finder to retrieve the latest products in our database, injecting a stored in several warehouses field on the fly, once the results are available.

Getting ready

We'll assume that you have the Package and Warehouse models and tables in place from our previous recipe, with the Package hasAndBelongsToMany Warehouse association.

How to do it...

Perform the following steps:

  1. First,...