CakePHP: Join results from multiple tables

Have you ever wanted to expand on your pre-set model queries with some table joins or other fancy footwork? You can join your model query to another table or tables like so:

$market = $this->Market->find('list',
		'joins' => array(
			'table' => 'companies_markets',
			'alias' => 'CompaniesMarkets',
			'type' => 'left',
			'foreignKey' => 'CompaniesMarkets.market_id',
			'conditions'=> array('CompaniesMarkets.market_id = Market.market_id')
	'conditions' => array(
			'CompaniesMarkets.company_id' => $company

What’s we’re doing here is specifying the ‘joins’ option along with a slew of other pieces to designate what to join, how, and by which field. The ‘conditions’ simply sets which fields I want to check for a certain value and which fields I want to use for this list.

Be Sociable, Share!