Get back query of a find
written on March 21, 2013There are times when you have to either query your database multiple time to achive a complex query or you can get back the command from cake and integrate it in another query. This second way will for sure increase your performance dramatically.
$db = $this->Device->getDataSource();
$cSubQuery = $db->buildStatement(
array(
'fields' => array('`Device`.`ownerUuid`'),
'table' => $db->fullTableName($this->Device),
'alias' => 'Device',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => array('Device.msisdn LIKE' => $countryFilters[$cnt].'%'),
'order' => null,
'group' => null
),
$this->Device
);
now the $cSubQuery
variable includes the full query string, it can be used for a sub query like this:
$cSubQuery = '( `User`.`country` = \''.$cnt.'\' OR `User`.`uuid` IN (' . $cSubQuery . ') )';
$countryFilterForUser = $db->expression($cSubQuery);
and $countryFilterForUser
can be passed to conditions key in a find query to filter the results.