Book Image

CakePHP 2 Application Cookbook - Third Edition

By : Watts
Book Image

CakePHP 2 Application Cookbook - Third Edition

By: Watts

Overview of this book

If you are a CakePHP developer looking to ease the burden of development, then this book is for you. As a headfirst dive into the framework, this collection of recipes will help you get the most out of CakePHP, and get your applications baked in no time. Even if you're not familiar with the framework, we'll take you from basic CRUD building to useful solutions that will aid in getting the job done quickly and efficiently.
Table of Contents (14 chapters)
13
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,...