When we are querying a model that has other associated models, CakePHP will issue a new query to fetch the associated data, or use a LEFT JOIN
SQL statement if the associated model has a one-to-one relationship with the main model (through a binding defined with belongsTo
or hasOne
.)
However there are times where we need to change the join type for one-to-one associations, to use either a RIGHT JOIN
or an INNER JOIN
. This recipe shows us how to change the join type for belongsTo
and hasOne
associations.
Follow the Getting ready section of the recipe, Limiting the bindings returned in a find.
1. Edit the
Person
model, and change the binding definitions forbelongsTo
andhasOne
associations, as shown below:<?php class Person extends AppModel { public $belongsTo = array('Family' => array('type' => 'INNER')); public $hasOne = array('Profile' => array('type' => 'RIGHT')); public $hasMany = array('Post...