Creating accesor methods for virtual fields and replacing direct access from DboSource with new methods

This commit is contained in:
José Lorenzo Rodríguez 2009-12-06 22:16:00 -04:30
parent e7ee5e9eda
commit 4b9e03c9f7
2 changed files with 43 additions and 13 deletions

View file

@ -408,7 +408,7 @@ class DboSource extends DataSource {
retrun;
}
$model = ClassRegistry::getObject($alias);
if (isset($model->virtualFields[$virtual])) {
if ($model->isVirtualField($virtual)) {
$result[$alias][$virtual] = $value;
unset($result[0][$field]);
}
@ -1667,8 +1667,8 @@ class DboSource extends DataSource {
if (!isset($params[1])) {
$params[1] = 'count';
}
if (!empty($model->virtualFields[$params[0]])) {
$arg = $model->virtualFields[$params[0]];
if (is_object($model) && $model->isVirtualField($params[0])){
$arg = $model->getVirtualField($params[0]);
} else {
$arg = $this->name($params[0]);
}
@ -1678,8 +1678,8 @@ class DboSource extends DataSource {
if (!isset($params[1])) {
$params[1] = $params[0];
}
if (!empty($model->virtualFields[$params[0]])) {
$arg = $model->virtualFields[$params[0]];
if (is_object($model) && $model->isVirtualField($params[0])) {
$arg = $model->getVirtualField($params[0]);
} else {
$arg = $this->name($params[0]);
}
@ -1816,7 +1816,7 @@ class DboSource extends DataSource {
$virtual = array();
foreach ($fields as $field) {
$virtualField = $this->name("{$alias}__{$field}");
$expression = $model->virtualFields[$field];
$expression = $model->getVirtualField($field);
$virtual[] = $expression . " {$this->alias} {$virtualField}";
}
return $virtual;
@ -1865,8 +1865,8 @@ class DboSource extends DataSource {
return $fields;
}
$virtual = array();
if (!empty($model->virtualFields)) {
$keys = array_keys($model->virtualFields);
if ($model->getVirtualField()) {
$keys = array_keys($model->getVirtualField());
$virtual = ($allFields) ? $keys : array_intersect($keys,$fields);
}
$count = count($fields);
@ -2114,8 +2114,8 @@ class DboSource extends DataSource {
}
$virtual = false;
if (!empty($model->virtualFields[$key])) {
$key = $model->virtualFields[$key];
if (is_object($model) && $model->isVirtualField($key)) {
$key = $model->getVirtualField($key);
$virtual = true;
}
@ -2287,8 +2287,8 @@ class DboSource extends DataSource {
$key = trim($key);
if (!preg_match('/\s/', $key) && !strpos($key,'.')) {
if (!empty($model->virtualFields[$key])) {
$key = $model->virtualFields[$key];
if (is_object($model) && $model->isVirtualField($key)) {
$key = $model->getVirtualField($key);
} else {
$key = $this->name($key);
}

View file

@ -1026,7 +1026,7 @@ class Model extends Overloadable {
}
if ($checkVirtual && !empty($this->virtualFields)) {
if (array_key_exists($name,$this->virtualFields)) {
if ($this->isVirtualField($name)) {
return true;
}
}
@ -1041,6 +1041,36 @@ class Model extends Overloadable {
return false;
}
/**
* Returns true if the supplied field is a model Virtual Field
*
* @param mixed $name Name of field to look for
* @return boolean indicating whether the field exists as a model virtual field.
* @access public
*/
function isVirtualField($field) {
return !empty($this->virtualFields) && array_key_exists($field,$this->virtualFields);
}
/**
* Returns the expression for a model virtual field
*
* @param mixed $name Name of field to look for
* @return mixed If $field is string expression bound to virtual field $field
* If $field is null, returns an array of all model virtual fields
* or false if none $field exist.
* @access public
*/
function getVirtualField($field = null) {
if ($field == null) {
return empty($this->virtualFields) ? false : $this->virtualFields;
}
if ($this->isVirtualField($field)) {
return $this->virtualFields[$field];
}
return false;
}
/**
* Initializes the model for writing a new record, loading the default values
* for those fields that are not defined in $data, and clearing previous validation errors.