From 282bfbf4fcb4a115b39777bd488ff4058d302baa Mon Sep 17 00:00:00 2001 From: nate Date: Mon, 26 Mar 2007 18:38:54 +0000 Subject: [PATCH] Fixing SQL expression handling in Model::field() (TIcket #2283) git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4684 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/model.php | 9 ++++----- cake/tests/cases/libs/model/model.test.php | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 0b5f4c359..3fc22d25e 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -957,18 +957,17 @@ class Model extends Overloadable { if (strpos($name, '.') === false) { if (isset($data[$this->name][$name])) { return $data[$this->name][$name]; - } else { - return false; } } else { $name = explode('.', $name); - if (isset($data[$name[0]][$name[1]])) { return $data[$name[0]][$name[1]]; - } else { - return false; } } + if (isset($data[0]) && count($data[0]) > 0) { + $name = key($data[0]); + return $data[0][$name]; + } } else { return false; } diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 71ee896d1..9208a45b7 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -254,7 +254,25 @@ class ModelTest extends CakeTestCase { $expected = array(); $this->assertEqual($result, $expected); } - + + function testFindField() { + $this->model =& new User(); + + $this->model->id = 1; + $result = $this->model->field('user'); + $this->assertEqual($result, 'mariano'); + + $result = $this->model->field('User.user'); + $this->assertEqual($result, 'mariano'); + + $this->model->id = false; + $result = $this->model->field('user', array('user' => 'mariano')); + $this->assertEqual($result, 'mariano'); + + $result = $this->model->field('COUNT(*) AS count', true); + $this->assertEqual($result, 4); + } + function testBindUnbind() { $this->model =& new User();