From 8c4cad86c2a0b09642d1376a1c403ab8485cb83c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Thu, 10 Dec 2009 20:13:09 -0430 Subject: [PATCH] Fixing erroris due to bad rebase --- cake/libs/model/datasources/dbo_source.php | 47 ----- cake/libs/model/model.php | 8 +- .../cases/libs/model/model_read.test.php | 160 +++++------------- 3 files changed, 47 insertions(+), 168 deletions(-) diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index 3d349ed1d..e49f61473 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -419,35 +419,6 @@ class DboSource extends DataSource { } } -/** - * Modifies $result array to place virtual fields in model entry where they belongs to - * - * @param array $resut REference to the fetched row - * @return void - */ - function fetchVirtualField(&$result) { - if (isset($result[0]) && is_array($result[0])) { - foreach ($result[0] as $field => $value) { - if (strpos($field,'__') === false) { - continue; - } - list($alias,$virtual) = explode('__',$field); - - if (!ClassRegistry::isKeySet($alias)) { - retrun; - } - $model = ClassRegistry::getObject($alias); - if (isset($model->virtualFields[$virtual])) { - $result[$alias][$virtual] = $value; - unset($result[0][$field]); - } - } - if (empty($result[0])) { - unset($result[0]); - } - } - } - /** * Returns a single field of the first of query results for a given SQL query, or false if empty. * @@ -1822,24 +1793,6 @@ class DboSource extends DataSource { return $virtual; } -/** - * Converts model virtual fields into sql expressions to be fetched later - * - * @param Model $model - * @param string $alias Alias tablename - * @param mixed $fields virtual fields to be used on query - * @return array - */ - function _constructVirtualFields(&$model,$alias,$fields) { - $virtual = array(); - foreach ($fields as $field) { - $virtualField = $this->name("{$alias}__{$field}"); - $expression = $model->virtualFields[$field]; - $virtual[] = $expression . " {$this->alias} {$virtualField}"; - } - return $virtual; - } - /** * Generates the fields list of an SQL query. * diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 27814c245..494af6317 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1049,7 +1049,7 @@ class Model extends Overloadable { * @access public */ function isVirtualField($field) { - return !empty($this->virtualFields) && array_key_exists($field,$this->virtualFields); + return !empty($this->virtualFields) && is_string($field) && array_key_exists($field,$this->virtualFields); } /** @@ -1170,9 +1170,9 @@ class Model extends Overloadable { return $data[$name[0]][$name[1]]; } } - if (!empty($data[0])) { - $name = key($data[0]); - return $data[0][$name]; + if (isset($data[0]) && count($data[0]) > 0) { + return array_shift($data[0]); + } } else { return false; } diff --git a/cake/tests/cases/libs/model/model_read.test.php b/cake/tests/cases/libs/model/model_read.test.php index ddc174e41..bd37c9018 100644 --- a/cake/tests/cases/libs/model/model_read.test.php +++ b/cake/tests/cases/libs/model/model_read.test.php @@ -26,123 +26,6 @@ require_once dirname(__FILE__) . DS . 'model.test.php'; */ class ModelReadTest extends BaseModelTest { -/** - * testVirtualFields() - * - * Test correct fetching of virtual fields - * currently is not possible to do Relation.virtualField - * - * @access public - * @return void - */ - function testVirtualFields() { - $this->loadFixtures('Post','Author'); - $Post = ClassRegistry::init('Post'); - $Post->virtualFields = array('two' => "1 + 1"); - $result = $Post->find('first'); - $this->assertEqual($result['Post']['two'],2); - - $Post->Author->virtualFields = array('false' => '1 = 2'); - $result = $Post->find('first'); - $this->assertEqual($result['Post']['two'],2); - $this->assertEqual($result['Author']['false'],false); - - $result = $Post->find('first',array('fields' => array('author_id'))); - $this->assertFalse(isset($result['Post']['two'])); - $this->assertFalse(isset($result['Author']['false'])); - - $result = $Post->find('first',array('fields' => array('author_id','two'))); - $this->assertEqual($result['Post']['two'],2); - $this->assertFalse(isset($result['Author']['false'])); - - $result = $Post->find('first',array('fields' => array('two'))); - $this->assertEqual($result['Post']['two'],2); - - $Post->id = 1; - $result = $Post->field('two'); - $this->assertEqual($result,2); - - $result = $Post->find('first',array( - 'conditions' => array('two' => 2), - 'limit' => 1 - )); - $this->assertEqual($result['Post']['two'],2); - - $result = $Post->find('first',array( - 'conditions' => array('two <' => 3), - 'limit' => 1 - )); - $this->assertEqual($result['Post']['two'],2); - - $result = $Post->find('first',array( - 'conditions' => array('NOT' => array('two >' => 3)), - 'limit' => 1 - )); - $this->assertEqual($result['Post']['two'],2); - - $dbo =& $Post->getDataSource(); - $Post->virtualFields = array('other_field' => $dbo->name('Post.id') . ' + 1'); - $result = $Post->find('first',array( - 'conditions' => array('other_field' => 3), - 'limit' => 1 - )); - $this->assertEqual($result['Post']['id'],2); - - $Post->virtualFields = array('other_field' => $dbo->name('Post.id') . ' + 1'); - $result = $Post->find('all',array( - 'fields' => array($dbo->calculate($Post,'max',array('other_field'))) - )); - $this->assertEqual($result[0][0]['other_field'],4); - - ClassRegistry::flush(); - $Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'),'Model'); - $Writing->virtualFields = array('two' => "1 + 1"); - $result = $Writing->find('first'); - $this->assertEqual($result['Writing']['two'],2); - } - -/** - * testVirtualFields() - * - * Test correct fetching of virtual fields - * currently is not possible to do Relation.virtualField - * - * @access public - * @return void - */ - function testVirtualFields() { - $this->loadFixtures('Post','Author'); - $Post = ClassRegistry::init('Post'); - $Post->virtualFields = array('two' => "1 + 1"); - $result = $Post->find('first'); - $this->assertEqual($result['Post']['two'],2); - - $Post->Author->virtualFields = array('false' => '1 = 2'); - $result = $Post->find('first'); - $this->assertEqual($result['Post']['two'],2); - $this->assertEqual($result['Author']['false'],false); - - $result = $Post->find('first',array('fields' => array('author_id'))); - $this->assertFalse(isset($result['Post']['two'])); - $this->assertFalse(isset($result['Author']['false'])); - - $result = $Post->find('first',array('fields' => array('author_id','two'))); - $this->assertEqual($result['Post']['two'],2); - $this->assertFalse(isset($result['Author']['false'])); - - $result = $Post->find('first',array('fields' => array('two'))); - $this->assertEqual($result['Post']['two'],2); - - $Post->id = 1; - $result = $Post->field('two'); - $this->assertEqual($result,2); - - ClassRegistry::flush(); - $Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'),'Model'); - $Writing->virtualFields = array('two' => "1 + 1"); - $result = $Writing->find('first'); - $this->assertEqual($result['Writing']['two'],2); - } /** * testFetchingNonUniqueFKJoinTableRecords() @@ -7279,5 +7162,48 @@ class ModelReadTest extends BaseModelTest { $comments = $Comment->find('first'); $this->assertEqual($comments['Comment']['querytype'], 'first'); } + +/** + * testVirtualFields() + * + * Test correct fetching of virtual fields + * currently is not possible to do Relation.virtualField + * + * @access public + * @return void + */ + function testVirtualFields() { + $this->loadFixtures('Post','Author'); + $Post = ClassRegistry::init('Post'); + $Post->virtualFields = array('two' => "1 + 1"); + $result = $Post->find('first'); + $this->assertEqual($result['Post']['two'],2); + + $Post->Author->virtualFields = array('false' => '1 = 2'); + $result = $Post->find('first'); + $this->assertEqual($result['Post']['two'],2); + $this->assertEqual($result['Author']['false'],false); + + $result = $Post->find('first',array('fields' => array('author_id'))); + $this->assertFalse(isset($result['Post']['two'])); + $this->assertFalse(isset($result['Author']['false'])); + + $result = $Post->find('first',array('fields' => array('author_id','two'))); + $this->assertEqual($result['Post']['two'],2); + $this->assertFalse(isset($result['Author']['false'])); + + $result = $Post->find('first',array('fields' => array('two'))); + $this->assertEqual($result['Post']['two'],2); + + $Post->id = 1; + $result = $Post->field('two'); + $this->assertEqual($result,2); + + ClassRegistry::flush(); + $Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'),'Model'); + $Writing->virtualFields = array('two' => "1 + 1"); + $result = $Writing->find('first'); + $this->assertEqual($result['Writing']['two'],2); + } } ?> \ No newline at end of file