You need to enable pent-up behavior in your models or even better install it in your application model.
public $actsAs = array('Containable');
And then start creating your queries, for example.
$conditions = array( 'conditions' => array('id' => '123'), 'contain' => array( 'Neighbourhood'=>array( 'conditions' => array('Neighbourhood.id' => '123') ) ), // or even joins 'joins' => array( array( 'table' => $this->getTableName('default', 'neighbourhoods'), 'alias' => 'Neighbourhood', 'type' => 'RIGHT', // OR LEFT 'conditions' => array( 'Neighbourhood.id = Polygon.neighbourhood_id', 'Neighbourhood.deleted' => 0, ) ) ) ); $polygons = $this->Polygon->find('all', $conditions);
If you think this is not enough to fulfill more complex queries, you will need to create your own queries. For example, starting a query from a Polygon model:
$dbo = $this->getDataSource(); $query = $dbo->buildStatement( array( 'fields' => array( 'Polygon.name AS polygon_name', 'Polygon.size', 'Neighbourhood.name AS neighbourhood_name', 'Neighbourhood.lat', 'IF( Neighbourhood.created > DATE_SUB(NOW(), INTERVAL 1 DAY) , 1 , 0) AS new_neighbourhood' ), 'table' => $dbo->fullTableName($this), 'alias' => 'Polygon', 'limit' => null, 'offset' => null, 'joins' => array( array( 'table' => $this->getTableName('default', 'neighbourhoods'), 'alias' => 'Neighbourhood', 'type' => 'LEFT', 'conditions' => array( 'Neighbourhood.id = Polygon.neighbourhood_id', ), 'order' => 'Neighbourhood.name ASC', ), .... ), 'conditions' => $conditions, 'group' => 'Polygon.name', 'order' => 'Polygon.name ASC', ), $this ); $polygons = $this->query($query);
And if you think this is not enough, then you should sing an amazing grace like that.
$polygons = $this->query("Here your sql query"); debug($polygons);