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

Joining through


Sometimes, you'll need to store additional data related to a HABTM association between your models. In this case, you can use a join-through association type, using a new model to hold the association details.

Join-through associations are similar to HABTM, but instead of having one table that stores the IDs of the associated models, we also store additional data related to the specific association. For example, if we follow our package's HABTM warehouses example from the previous recipe, we might want to store the number of packages we have stored in each warehouse, thus adding a new field to our association table to specify the required stock number for each association,.

Getting ready

Let's define a stock per package in each warehouse and redefine our HABTM association from our previous recipe to use a join-through association instead.

Note that we are using the tables and models created in our previous recipe.

How to do it...

Perform the following steps:

  1. First, add a new amount...