I understand that this has been asked 100,000 times but I\'ve pretty much read all 100,000 replies and none of them seem to match what I\'m after. I\'ve tried every possible
Look at the SQL debug output, the regions are being retrieved using separate queries, that's how Cakes ORM currently works.
In your case there's a relatively simple workaround. Just create a proper association on the fly, for example Profile belongsTo Region
.
Here's an (untested) example, it adds a belongsTo
association with the foreignKey
option set to false
in order to disable the automatic foreign key generation by the ORM, this is necessary as it would otherwise look for something like Profile.region_id
. Note that consequently the contain
config has to be changed too!
$this->Profile->bindModel(array(
'belongsTo' => array(
'Region' => array(
'foreignKey' => false,
'conditions' => array('Region.id = Store.region_id')
),
)
));
$this->Paginator->settings = array(
'conditions' => array('Profile.job_title_id' => '1'),
'contain' => array('Store', 'Region')
);
That should generate a proper query that includes the stores
as well as the regions
table, making it possible to sort on Region.name
. Note that the resulting array will be a little different, Region
will not be nested in Store
, everything will be placed in the same level, ie:
Array
(
[Profile] => Array
(
....
)
[Store] => Array
(
....
)
[Region] => Array
(
....
)
)
Either you modify your view code accordingly, or you transform the retrieved data before passing it to the view. A third option would be to include a nested Region
in the contain
config, however that would result in additional queries that are totally unnecessary as the data was already fetched with the main query, so I wouldn't recommend that.