Monday, December 19, 2011

Using CakePHP Virtual Fields on the Fly

CakePHP virtualFields are awesome. Here is an example of using them on the fly. This code returns an value label pair for a Cake Form Helper Select List. function getBudgetItemSelectionList(){ $this->virtualFields['select_list_value'] = 'CONCAT(BudgetItem.name,if(BudgetCategory.income = 1," (Income)",""))'; return $this->find('list',array( 'fields'=>array('BudgetItem.id','BudgetItem.select_list_value','Budget.name'), 'order'=>'BudgetItem.name ASC', 'recursive'=>0, 'conditions'=>array('Budget.state_id'=>'0') )); }

No comments: