Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Making Model::isVirtualField() and Model::getVirtualField() work with…

… both `Model.field` as `field`. This fixes a number of issues in conditions, and fields arrays when model aliases are used. Fixes #208 and #220
  • Loading branch information...
commit e1b0c41254addfae5471e57a0579a2064965e7a1 1 parent 49ad582
@markstory markstory authored
View
2  cake/libs/model/datasources/dbo_source.php
@@ -2120,7 +2120,7 @@ function conditionKeysToString($conditions, $quoteValues = true, $model = null)
* @return string
* @access private
*/
- function __parseKey($model, $key, $value) {
+ function __parseKey(&$model, $key, $value) {
$operatorMatch = '/^((' . implode(')|(', $this->__sqlOps);
$operatorMatch .= '\\x20)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)/is';
$bound = (strpos($key, '?') !== false || (is_array($value) && strpos($key, ':') !== false));
View
18 cake/libs/model/model.php
@@ -1077,7 +1077,19 @@ function hasField($name, $checkVirtual = false) {
* @access public
*/
function isVirtualField($field) {
- return !empty($this->virtualFields) && is_string($field) && array_key_exists($field, $this->virtualFields);
+ if (empty($this->virtualFields) || !is_string($field)) {
+ return false;
+ }
+ if (isset($this->virtualFields[$field])) {
+ return true;
+ }
+ if (strpos($field, '.') !== false) {
+ list($model, $field) = explode('.', $field);
+ if (isset($this->virtualFields[$field])) {
+ return true;
+ }
+ }
+ return false;
}
/**
@@ -1094,6 +1106,9 @@ function getVirtualField($field = null) {
return empty($this->virtualFields) ? false : $this->virtualFields;
}
if ($this->isVirtualField($field)) {
+ if (strpos($field, '.') !== false) {
+ list($model, $field) = explode('.', $field);
+ }
return $this->virtualFields[$field];
}
return false;
@@ -2126,6 +2141,7 @@ function find($conditions = null, $fields = array(), $order = null, $recursive =
if (!$db =& ConnectionManager::getDataSource($this->useDbConfig)) {
return false;
}
+
$results = $db->read($this, $query);
$this->resetAssociations();
View
35 cake/tests/cases/libs/model/model_read.test.php
@@ -7172,7 +7172,7 @@ function testFindQueryTypeInCallbacks() {
* @return void
*/
function testVirtualFields() {
- $this->loadFixtures('Post','Author');
+ $this->loadFixtures('Post', 'Author');
$Post =& ClassRegistry::init('Post');
$Post->virtualFields = array('two' => "1 + 1");
$result = $Post->find('first');
@@ -7235,7 +7235,7 @@ function testVirtualFields() {
$Writing->virtualFields = array('two' => "1 + 1");
$result = $Writing->find('first');
$this->assertEqual($result['Writing']['two'], 2);
-
+
$Post->create();
$Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
$result = $Post->field('other_field');
@@ -7264,5 +7264,36 @@ function testVirtualFields() {
$this->assertEqual($result, $expectation);
}
+
+/**
+ * test that isVirtualField will accept both aliased and non aliased fieldnames
+ *
+ * @return void
+ */
+ function testIsVirtualField() {
+ $this->loadFixtures('Post');
+ $Post =& ClassRegistry::init('Post');
+ $Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
+
+ $this->assertTrue($Post->isVirtualField('other_field'));
+ $this->assertTrue($Post->isVirtualField('Post.other_field'));
+ $this->assertFalse($Post->isVirtualField('id'));
+ $this->assertFalse($Post->isVirtualField('Post.id'));
+ $this->assertFalse($Post->isVirtualField(array()));
+ }
+
+/**
+ * test that getting virtual fields works with and without model alias attached
+ *
+ * @return void
+ */
+ function testGetVirtualField() {
+ $this->loadFixtures('Post');
+ $Post =& ClassRegistry::init('Post');
+ $Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
+
+ $this->assertEqual($Post->getVirtualField('other_field'), $Post->virtualFields['other_field']);
+ $this->assertEqual($Post->getVirtualField('Post.other_field'), $Post->virtualFields['other_field']);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.