Cakephp 2.6如何在cakephp中運行子查詢
我有一個Model,Temps,它有很多票。在我希望爲每個記錄返回的Temps的索引視圖中,日期最接近當前日期的票證。
在MySQL中是可以做到的
'SELECT expiry_date FROM uploads WHERE expiry_date > CURDATE() ORDER BY expiry_date ASC LIMIT 1'
但我不知道如何作爲一個子查詢運行此。我目前的產生我的成績查詢如下:(銘記這已被配置爲數據表)門票是用於上傳模式
public function getAjaxIndexData($data) {
$tokens = explode(" ", $data['searchString']);
$conditions = array(
$this->alias . '.deleted' => false,
'OR' => array(
'CONCAT(' . $this->alias . '.first_name," ",' . $this->alias . '.last_name) LIKE' => '%' . implode(' ', $tokens) . '%',
),
$data['columnsFilter']
);
$fields = array(
'id',
'full_name',
'pps_number',
'mobile',
'email',
'start_date',
'time_served'
);
$order = array(
$data['orderField'] => $data['order']
);
$contain = array(
'LocalOffice.name',
);
$options = array(
'conditions' => $conditions,
'fields' => $fields,
'order' => $order,
'contain' => $contain,
'limit' => $data['limit'],
'offset' => $data['start']
);
$optionsNoFields = array(
'conditions' => $conditions,
'contain' => $contain,
);
$result['draw'] = $data['draw'];
$result['recordsTotal'] = $recordTotal = $this->find('count');
$result['recordsFiltered'] = $this->find('count', $optionsNoFields);
$result['data'] = $this->find('all', $options); //standard search
$result['data'] = $this->formatTable($result['data']);
return json_encode($result);
}
在此查詢我想添加一個字段的別名,顯示每個溫度最近的有效期限。
我該如何構造這個?
看一看http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries – bish