Skip to content

Commit

Permalink
Fix wrong data\source\Database field mapping when using custom fiel…
Browse files Browse the repository at this point in the history
…ds in queries.
  • Loading branch information
jails committed Dec 19, 2012
1 parent c0a3e2b commit 613955d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
7 changes: 7 additions & 0 deletions data/source/Database.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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['*'])) {
Expand Down Expand Up @@ -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);
Expand All @@ -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['*'])) {
Expand Down Expand Up @@ -984,6 +990,7 @@ protected function _fields($fields, $context) {
}
return $list;
}

protected function _fieldsQuote($alias, $field) {
$open = $this->_quotes[0];
$close = $this->_quotes[1];
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/data/model/QueryTest.php
Expand Up @@ -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,
Expand Down
29 changes: 24 additions & 5 deletions tests/cases/data/source/DatabaseTest.php
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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']);
}

Expand All @@ -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(
Expand All @@ -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']);
}

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 613955d

Please sign in to comment.