Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #756 from jails/pr14

Fix wrong `data\source\Database` field mapping when using custom fields in queries.
  • Loading branch information...
commit 8fe7f6a9eba5922a9f4a88741b53d4e39d8dce43 2 parents 5975540 + 613955d
@nateabele nateabele authored
View
7 data/source/Database.php
@@ -716,6 +716,7 @@ public function _schema($query, $fields = null) {
$model = $query->model();
$paths = $query->paths($this);
$models = $query->models($this);
+ $alias = $query->alias();
$result = array();
if (!$model) {
@@ -749,6 +750,8 @@ public function _schema($query, $fields = null) {
unset($fields[0]);
}
+ $fields = isset($fields[$alias]) ? array($alias => $fields[$alias]) + $fields : $fields;
+
foreach ($fields as $field => $value) {
if (is_array($value)) {
if (isset($value['*'])) {
@@ -920,6 +923,7 @@ public function _processConditions($key, $value, $context, $schema = null, $glue
public function fields($fields, $context) {
$type = $context->type();
$schema = $context->schema()->fields();
+ $alias = $context->alias();
if (!is_array($fields)) {
return $this->_fieldsReturn($type, $context, $fields, $schema);
@@ -937,6 +941,8 @@ public function fields($fields, $context) {
unset($fields[0]);
}
+ $fields = isset($fields[$alias]) ? array($alias => $fields[$alias]) + $fields : $fields;
+
foreach ($fields as $field => $value) {
if (is_array($value)) {
if (isset($value['*'])) {
@@ -984,6 +990,7 @@ protected function _fields($fields, $context) {
}
return $list;
}
+
protected function _fieldsQuote($alias, $field) {
$open = $this->_quotes[0];
$close = $this->_quotes[1];
View
2  tests/cases/data/model/QueryTest.php
@@ -677,7 +677,7 @@ public function testExportWithJoinedStrategy() {
'alias' => 'AS {MyAlias}',
'comment' => '/* No comment */',
'conditions' => 'WHERE {MyAlias}.{id} = 2',
- 'fields' => '{Tag}.*, {MyAlias}.{id}, {Image}.{id}, {ImageTag}.{id}',
+ 'fields' => '{MyAlias}.{id}, {Tag}.*, {Image}.{id}, {ImageTag}.{id}',
'having' => '',
'group' => null,
'order' => null,
View
29 tests/cases/data/source/DatabaseTest.php
@@ -1421,7 +1421,7 @@ public function testExportedFieldsWithJoinedStrategy() {
'with' => array('Image.ImageTag.Tag')
));
$result = $query->export($this->db);
- $expected = '{Tag}.{id}, {Gallery}.{id}, {Image}.{id}, {ImageTag}.{id}';
+ $expected = '{Gallery}.{id}, {Tag}.{id}, {Image}.{id}, {ImageTag}.{id}';
$this->assertEqual($expected, $result['fields']);
$query = new Query(array(
@@ -1430,7 +1430,7 @@ public function testExportedFieldsWithJoinedStrategy() {
'with' => array('Image.ImageTag.Tag')
));
$result = $query->export($this->db);
- $expected = '{Tag}.*, {Gallery}.{id}, {Image}.{id}, {ImageTag}.{id}';
+ $expected = '{Gallery}.{id}, {Tag}.*, {Image}.{id}, {ImageTag}.{id}';
$this->assertEqual($expected, $result['fields']);
$query = new Query(array(
@@ -1439,7 +1439,7 @@ public function testExportedFieldsWithJoinedStrategy() {
'with' => array('Image.ImageTag.Tag')
));
$result = $query->export($this->db);
- $expected = '{Tag}.*, {Gallery}.{id}, {Image}.{id}, {ImageTag}.{id}';
+ $expected = '{Gallery}.{id}, {Tag}.*, {Image}.{id}, {ImageTag}.{id}';
$this->assertEqual($expected, $result['fields']);
}
@@ -1458,7 +1458,7 @@ public function testExportedFieldsWithJoinedStrategyAndRecursiveRelation() {
'with' => array('Parent.Parent')
));
$result = $query->export($this->db);
- $expected = '{Parent}.{name}, {Gallery}.{id}';
+ $expected = '{Gallery}.{id}, {Parent}.{name}';
$this->assertEqual($expected, $result['fields']);
$query = new Query(array(
@@ -1467,7 +1467,7 @@ public function testExportedFieldsWithJoinedStrategyAndRecursiveRelation() {
'with' => array('Parent.Parent' => array('alias' => 'ParentOfParent'))
));
$result = $query->export($this->db);
- $expected = '{ParentOfParent}.{name}, {Gallery}.{id}, {Parent}.{id}';
+ $expected = '{Gallery}.{id}, {ParentOfParent}.{name}, {Parent}.{id}';
$this->assertEqual($expected, $result['fields']);
}
@@ -1502,6 +1502,25 @@ public function testCustomField() {
$result = $this->db->read($query);
$this->assertEqual($expected, $this->db->sql);
$this->assertEqual($map, $query->map());
+
+ $query = new Query(array(
+ 'type' => 'read',
+ 'model' => $this->_gallery,
+ 'fields' => array('count(Image.id) as count', 'Image'),
+ 'group' => 'Gallery.id',
+ 'with' => array('Image')
+ ));
+ $result = $this->db->read($query);
+ $expected = 'SELECT count(Image.id) as count, {Gallery}.{id}, {Image}.* FROM ';
+ $expected .= '{mock_gallery} AS {Gallery} LEFT JOIN {mock_image} AS {Image} ON ';
+ $expected .= '{Gallery}.{id} = {Image}.{gallery_id} GROUP BY Gallery.id;';
+
+ $this->assertEqual($expected, $this->db->sql);
+ $map = array(
+ '' => array('count', 'id'),
+ 'Image' => array('id', 'title', 'image', 'gallery_id')
+ );
+ $this->assertEqual($map, $query->map());
}
public function testReturnArrayOnReadWithString() {
Please sign in to comment.
Something went wrong with that request. Please try again.