diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index ed5bbbf1d..6d718e3f1 100755 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -1923,7 +1923,7 @@ class DboSource extends DataSource { foreach ($fields as $field) { $virtualField = $this->name($alias . $this->virtualFieldSeparator . $field); $expression = $this->__quoteFields($model->getVirtualField($field)); - $virtual[] = '(' .$expression . ") {$this->alias} {$virtualField}"; + $virtual[] = '(' . $expression . ") {$this->alias} {$virtualField}"; } return $virtual; } diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index 442d690ac..f7598ba11 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -4359,6 +4359,30 @@ class DboSourceTest extends CakeTestCase { $this->assertEqual($expected, $result); } +/** + * test that virtualFields with complex functions and aliases work. + * + * @return void + */ + function testConditionsWithComplexVirtualFields() { + $Article =& ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'distance' => 'ACOS(SIN(20 * PI() / 180) + * SIN(Article.latitude * PI() / 180) + + COS(20 * PI() / 180) + * COS(Article.latitude * PI() / 180) + * COS((50 - Article.longitude) * PI() / 180) + ) * 180 / PI() * 60 * 1.1515 * 1.609344' + ); + + $conditions = array('distance >=' => 20); + $result = $this->db->conditions($conditions, true, true, $Article); + + $this->assertPattern('/\) >= 20/', $result); + $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result); + $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result); + } + /** * test order to generate query order clause for virtual fields * @@ -4432,6 +4456,33 @@ class DboSourceTest extends CakeTestCase { $this->assertEqual($expected, $result); } +/** + * test that virtualFields with complex functions and aliases work. + * + * @return void + */ + function testFieldsWithComplexVirtualFields() { + $Article =& new Article(); + $Article->virtualFields = array( + 'distance' => 'ACOS(SIN(20 * PI() / 180) + * SIN(Article.latitude * PI() / 180) + + COS(20 * PI() / 180) + * COS(Article.latitude * PI() / 180) + * COS((50 - Article.longitude) * PI() / 180) + ) * 180 / PI() * 60 * 1.1515 * 1.609344' + ); + + $fields = array('id', 'distance'); + $result = $this->db->fields($Article, null, $fields); + $qs = $this->db->startQuote; + $qe = $this->db->endQuote; + + $this->assertEqual($result[0], "{$qs}Article{$qe}.{$qs}id{$qe}"); + $this->assertPattern('/Article__distance/', $result[1]); + $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result[1]); + $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result[1]); + } + /** * test group to generate GROUP BY statements on virtual fields *