Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Creating accesor methods for virtual fields and replacing direct acce…

…ss from DboSource with new methods
  • Loading branch information...
commit 4b9e03c9f779622ddf6e308ea04454fe6fb90d2a 1 parent e7ee5e9
@lorenzo lorenzo authored
Showing with 43 additions and 13 deletions.
  1. +12 −12 cake/libs/model/datasources/dbo_source.php
  2. +31 −1 cake/libs/model/model.php
View
24 cake/libs/model/datasources/dbo_source.php
@@ -408,7 +408,7 @@ function fetchVirtualField(&$result) {
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 @@ function calculate(&$model, $func, $params = array()) {
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 @@ function calculate(&$model, $func, $params = array()) {
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 @@ function _constructVirtualFields(&$model,$alias,$fields) {
$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 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
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 @@ function __parseKey($model, $key, $value) {
}
$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 @@ function order($keys, $direction = 'ASC', &$model = null) {
$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
32 cake/libs/model/model.php
@@ -1026,7 +1026,7 @@ function hasField($name, $checkVirtual = false) {
}
if ($checkVirtual && !empty($this->virtualFields)) {
- if (array_key_exists($name,$this->virtualFields)) {
+ if ($this->isVirtualField($name)) {
return true;
}
}
@@ -1042,6 +1042,36 @@ function hasField($name, $checkVirtual = 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.
* Especially helpful for saving data in loops.
Please sign in to comment.
Something went wrong with that request. Please try again.