Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 1 commit into from

3 participants

@jails
Collaborator

Fix issue #403 and #414.

@nateabele nateabele merged commit e2afeca into from
@hans-d

when is this expected to to be merged in master?

@nateabele
Owner

Soon. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 24, 2012
  1. @jails

    Add by default the context alias on ambiguous fields on read context.…

    jails authored
    … Fix issue #403 and #414.
This page is out of date. Refresh to see the latest.
Showing with 32 additions and 17 deletions.
  1. +12 −15 data/source/Database.php
  2. +20 −2 tests/cases/data/source/DatabaseTest.php
View
27 data/source/Database.php
@@ -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;
@@ -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)):
@@ -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)])):
@@ -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";
- }
}
}
View
22 tests/cases/data/source/DatabaseTest.php
@@ -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(
@@ -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,
Something went wrong with that request. Please try again.