Some times we want to display in view combination of diferent fields of model. For example we want to have list box with full name (that combination of first_name last_name or prefix).
Also we want to paginate by this column (full name).
First class tasks is much more simple.
We can solve this problem in two way. First add column in after find callback, and after it we can use for examlpe $model->generateList method for such multiColumn. This method used in Autofield behavior.
Another class of tasks require sorting during fetching data from DB. In this case only one sollution possible. We need to add columns to fields list ib beforeFind callback. This method used in Truefield behavior.
<?php/*
* Autofield behavior for cakePHP
* comments, bug reports are welcome skie AT mail DOT ru
* @author Yevgeny Tomenko aka SkieDr
* @version 1.0.0.0
configuration is
mask is sprintf like mask
array (‘newFieldName’ => array(‘fields’=>array(‘field1′,’field2′), ’mask’=>’(%s,%s’),
’newFieldName2′ => array(‘fields’=>array(‘field1′,’field3′), ’mask’=>’%s: %s’)
);
*/ class AutoFieldBehavior extends ModelBehavior
{ var $settings = null;
//var $_model = null;
function setup(&$model, $config = array()) {
$this->autoFieldSetup(&$model, $config);
}
function AutoFieldSetup(&$model, $config = array()) {
//$config = array(‘enabled’=>true); foreach ($config as $newField => $fieldDsc
) {
if (isset($fieldDsc['fields'])){
foreach ($fieldDsc['fields'] as $field) {
if(!$model->hasField($field)) {
//user_error(“Model {$model->name} does not have a field called {$field}”, E_USER_ERROR);
}
}
}
if (!isset($config[$newField]['fields'])) {
$config[$newField]['fields']=array();
}
if (!isset($config[$newField]['mask'])) {
$config[$newField]['mask']=”;
}
} $this->settings[$model->name] = $config;
}
/**
* After find method. Called after all find
*
* Add aditional fields
*
* @param AppModel $model
* @return boolean True to continue, false to abort the save
*/
function afterFind(&$model, $results ) {
$config=$this->settings[$model->name];
if (is_array($results)) {
$i = 0;
while(isset($results[$i][$model->name]) && is_array($results[$i][$model->name])) {
if(count($results[$i][$model->name])>0) {
foreach ($config as $newField => $fieldDsc) {
$fieldVals=array();
foreach ($fieldDsc['fields'] as $field) {
if (isset($results[$i][$model->name][$field])) {
$fieldVals[]=$results[$i][$model->name][$field];
} else {
$fieldVals[]=”;
}
}
$results[$i][$model->name][$newField]=vsprintf($fieldDsc['mask'],$fieldVals);
}
}
$i++;
}
}
return $results;
} }
?>
<?php/*
* True Field behavior for cakePHP
* comments, bug reports are welcome skie AT mail DOT ru
* @author Yevgeny Tomenko aka SkieDr
* @version 1.0.0.0
configuration is
array ( //’newFieldName’ => ’natave sql function calls’,
’newFieldName2′ => ’concat(‘(‘,id,’, ’,numid,’)')’
);
*/
class TrueFieldBehavior extends ModelBehavior { var $settings = null
;
//var $_model = null;
function setup(&$model, $config = array()) {
$this->trueFieldSetup(&$model, $config);
}
function TrueFieldSetup(&$model, $config = array()) {
foreach ($config as $newField => $fieldDsc) {
} $this->settings[$model->name] = $config;
}
/**
* Before find method. Called before all find
*
* Set scope filer settings
*
* @param AppModel $model
* @return boolean True to continue, false to abort the save
*/
function beforeFind(&$model, $cond) {
$model->log(array(‘true columns: beforeFind’=>$cond, ‘conf’=>$this->settings[$model->name]));
$config=$this->settings[$model->name];
$conds=array();
foreach ($config as $newField => $fieldDsc) {
$conds[]=“$fieldDsc as {$model->name}__$newField”;
}
if (count($conds)>0) {
if (is_array($cond['fields'])) {
$cond['fields']=am($cond['fields'],$conds);
} elseif ($cond['fields']!=”) {
$cond['fields']=am($cond['fields'],$conds);
} else {
$cond['fields']=$conds;
}
$cond['fields'][]=“{$model->name}.*”;
foreach ($model->belongsTo as $assoc => $assocDsc) {
$cond['fields'][]=“$assoc.*”;
}
}
return $cond;
}
/**
* After find method. Called after all find
*
* Add aditional fields
*
* @param AppModel $model
* @return boolean True to continue, false to abort the save
*/
function afterFind(&$model, $results ) {
$config=$this->settings[$model->name];
if (is_array($results)) {
$i = 0;
while(isset($results[$i][$model->name]) && is_array($results[$i][$model->name])) {
foreach($results[$i] as $key => $value) {
if(is_numeric($key)) {
$cleanList=array();
foreach ($value as $field => $fieldVal) {
$fieldDsc = preg_split(‘/__/’,$field);
if (count($fieldDsc)==2) {
$results[$i][$fieldDsc[0]][$fieldDsc[1]]=$fieldVal;
$cleanList[]=$field;
}
}
foreach ($cleanList as $field) {
unset($results[$i][$key][$field]);
}
if (count($results[$i][$key])==0) {
unset($results[$i][$key]);
}
}
}
$i++;
}
}
return $results;
}
}
?>