我正在尝试使用cakephp 1.3中的cakephp virtualFields。我的sql查询如下,但我需要day_index(我的虚拟字段)为' DAYOFWEEK(start_date)'。
我需要重写一个查询
$data = $this->Calendar->query("SELECT *, DAYOFWEEK(start_date) as day_index, TIME(start_time) as time
FROM calendars WHERE calendar_category_id =$cal ORDER BY day_index, time");
采用以下格式:
$sqlConditions = array("Calendar.calendar_category_id"=>$cal);
$sqlOrderBy = array("Calendar.day_index", "Calendar.time asc");
$sqlParams = array('conditions'=>$sqlConditions,'order'=>$sqlOrderBy);
$data = $this->Calendar->find('all',$sqlParams);
$this->set('data',$data);
所以我不确定如何/在哪里放置或声明虚拟字段。
$fields = $this->Calendar->virtualFields['day_index'].'AS 'DAYOFWEEK(start_date)';
答案 0 :(得分:0)
找到两种方法来做到这一点,不确定一个是否优于另一个。 控制器仅编辑方法
$sqlFields = array("DAYOFWEEK(Calendar.start_date) as day_index",
"TIME(Calendar.start_time) as time",
"Calendar.start_date",
"Calendar.calendar_category_id",
"Calendar.start_time");
$sqlConditions = array("Calendar.calendar_category_id"=>$cal);
$sqlOrderBy = array("Calendar.day_index, Calendar.time asc");
$sqlParams = array('fields'=>$sqlFields,'conditions'=>$sqlConditions,'order'=>$sqlOrderBy);
$data = $this->Calendar->find('all',$sqlParams);
return $data;
exit();
基于此方法编辑控制器的模型方法 型号>
var $virtualFields = array(
'day_index' => "DAYOFWEEK(Calendar.start_date)",
'time' => "TIME(Calendar.start_time)"
);
var $displayIndex = 'day_index';
var $displayTime = 'time';
控制器>
$sqlConditions = array("Calendar.calendar_category_id"=>$cal);
$sqlOrderBy = array("Calendar.day_index, Calendar.time asc");
$sqlParams = array('conditions'=>$sqlConditions,'order'=>$sqlOrderBy);
$data = $this->Calendar->find('all',$sqlParams);
return $data;
exit();