php - cakephp Virtual Field in SQL query -
i trying work cakephp virtualfields in cakephp 1.3. sql query follows need day_index (my virtual fields) 'dayofweek(start_date)'.
i need rewrite query
$data = $this->calendar->query("select *, dayofweek(start_date) day_index, time(start_time) time calendars calendar_category_id =$cal order day_index, time");
into format:
$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);
so i'm not sure how to/where put or declare virtual field.
$fields = $this->calendar->virtualfields['day_index'].'as 'dayofweek(start_date)';
found 2 ways this, not sure if 1 better other. controller edit method
$sqlfields = array("dayofweek(calendar.start_date) day_index", "time(calendar.start_time) 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();
model method edits controller based on method model >
var $virtualfields = array( 'day_index' => "dayofweek(calendar.start_date)", 'time' => "time(calendar.start_time)" ); var $displayindex = 'day_index'; var $displaytime = 'time';
controller >
$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();
Comments
Post a Comment