Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add by default the context alias on ambiguous fields on read context. #482

Merged
merged 1 commit into from
May 24, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions data/source/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public function conditions($conditions, $context, array $options = array()) {
$result = array();

foreach ($conditions as $key => $value) {
$return = $this->_processConditions($key, $value, $schema);
$return = $this->_processConditions($key, $value, $context, $schema);

if ($return) {
$result[] = $return;
Expand All @@ -599,19 +599,25 @@ public function conditions($conditions, $context, array $options = array()) {
return ($options['prepend'] && $result) ? "WHERE {$result}" : $result;
}

public function _processConditions($key, $value, $schema, $glue = 'AND') {
public function _processConditions($key, $value, $context, $schema, $glue = 'AND') {
$constraintTypes =& $this->_constraintTypes;
$fieldMeta = $schema->fields($key) ?: array();

switch (true) {
case (is_numeric($key) && is_string($value)):
return $value;
case is_string($value):
return $this->name($key) . ' = ' . $this->value($value, $fieldMeta);
case is_scalar($value) || is_null($value):
if ($context->type() == 'read' && ($alias = $context->alias()) && !strpos($key, '.')) {
$key = $alias . "." . $key;
}
if (isset($value)) {
return $this->name($key) . ' = ' . $this->value($value, $fieldMeta);
}
return $this->name($key) . " IS NULL";
case is_numeric($key) && is_array($value):
$result = array();
foreach ($value as $cField => $cValue) {
$result[] = $this->_processConditions($cField, $cValue, $schema, $glue);
$result[] = $this->_processConditions($cField, $cValue, $context, $schema, $glue);
}
return '(' . implode(' ' . $glue . ' ', $result) . ')';
case (is_string($key) && is_object($value)):
Expand All @@ -622,7 +628,7 @@ public function _processConditions($key, $value, $schema, $glue = 'AND') {
$glue = strtoupper($key);

foreach ($value as $cField => $cValue) {
$result[] = $this->_processConditions($cField, $cValue, $schema, $glue);
$result[] = $this->_processConditions($cField, $cValue, $context, $schema, $glue);
}
return '(' . implode(' ' . $glue . ' ', $result) . ')';
case (is_string($key) && is_array($value) && isset($this->_operators[key($value)])):
Expand All @@ -633,15 +639,6 @@ public function _processConditions($key, $value, $schema, $glue = 'AND') {
case is_array($value):
$value = join(', ', $this->value($value, $fieldMeta));
return "{$this->name($key)} IN ({$value})";
default:
$key = $this->name($key);
if (isset($value)) {
$value = $this->value($value, $fieldMeta);
return "{$key} = {$value}";
}
if ($value === null) {
return "{$key} IS NULL";
}
}
}

Expand Down
22 changes: 20 additions & 2 deletions tests/cases/data/source/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,10 @@ public function testConditions() {
)
));
$sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE ";
$sql .= "({id} = 0 OR {title} = 'value2' OR ({author_id} = 1 AND {created} = '2')";
$sql .= " OR ({title} = 'value2') OR ({title} IS NULL)) AND {id} = 3 AND {author_id} = 0;";
$sql .= "({MockDatabasePost}.{id} = 0 OR {MockDatabasePost}.{title} = 'value2' OR ";
$sql .= "({MockDatabasePost}.{author_id} = 1 AND {MockDatabasePost}.{created} = '2') OR ";
$sql .= "({MockDatabasePost}.{title} = 'value2') OR ({MockDatabasePost}.{title} IS NULL)) AND ";
$sql .= "{MockDatabasePost}.{id} = 3 AND {MockDatabasePost}.{author_id} = 0;";
$this->assertEqual($sql, $this->db->renderCommand($query));

$query = new Query(array(
Expand All @@ -594,6 +596,22 @@ public function testConditions() {
$this->assertEqual($sql, $this->db->renderCommand($query));
}

public function testReadConditionsWithModel() {
$model = $this->_model;
$options = array(
'type' => 'read',
'model' => $this->_model,
'conditions' => array('id' => 1, 'MockDatabaseComment.id' => 2),
'with' => array('MockDatabaseComment')
);
$result = $this->db->read(new Query($options), $options);
$expected = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} LEFT JOIN ';
$expected .= '{mock_database_comments} AS {MockDatabaseComment} ON ';
$expected .= '{MockDatabasePost}.{id} = {MockDatabaseComment}.{mock_database_post_id} ';
$expected .= 'WHERE {MockDatabasePost}.{id} = 1 AND {MockDatabaseComment}.{id} = 2;';
$this->assertEqual($expected, $this->db->sql);
}

public function testFields() {
$query = new Query(array(
'model' => $this->_model,
Expand Down