Skip to content
This repository
Browse code

Merge pull request #617 from nealerickson/data

Fix in casting query conditions
  • Loading branch information...
commit 884e839bec426a09c889d39d7ef0cabe3a13ccab 2 parents baa62ee + 275e1bc
Nate Abele nateabele authored
24 data/source/Database.php
@@ -241,6 +241,26 @@ public function name($name) {
241 241 }
242 242
243 243 /**
  244 + * Return the field name from a conditions key.
  245 + *
  246 + * @param string $field Field or identifier name.
  247 + * @return string Returns the field name without the table alias, if applicable.
  248 + * @todo Eventually, this should be refactored and moved to the Query or Schema
  249 + * class. Also, by handling field resolution in this way we are not handling
  250 + * cases where query conditions use the same field name in multiple tables.
  251 + * e.g. Foos.bar and Bars.bar will both return bar.
  252 + */
  253 + protected function _fieldName($field) {
  254 + if (is_string($field)) {
  255 + if (preg_match('/^[a-z0-9_-]+\.[a-z0-9_-]+$/i', $field)) {
  256 + list($first, $second) = explode('.', $field, 2);
  257 + return $second;
  258 + }
  259 + }
  260 + return $field;
  261 + }
  262 +
  263 + /**
244 264 * Converts a given value into the proper type based on a given schema definition.
245 265 *
246 266 * @see lithium\data\source\Database::schema()
@@ -707,7 +727,7 @@ protected function _conditions($conditions, $context, array $options = array())
707 727
708 728 public function _processConditions($key, $value, $context, $schema, $glue = 'AND') {
709 729 $constraintTypes =& $this->_constraintTypes;
710   - $fieldMeta = $schema->fields($key) ?: array();
  730 + $fieldMeta = $schema->fields($this->_fieldName($key)) ?: array();
711 731
712 732 switch (true) {
713 733 case (is_numeric($key) && is_string($value)):
@@ -766,7 +786,7 @@ public function _processConditions($key, $value, $context, $schema, $glue = 'AND
766 786 */
767 787 public function fields($fields, $context) {
768 788 $type = $context->type();
769   - $schema = (array) $context->schema();
  789 + $schema = (array) $context->schema()->fields();
770 790 $modelNames = (array) $context->name();
771 791 $modelNames = array_merge($modelNames, array_keys((array) $context->relationships()));
772 792
57 tests/cases/data/source/DatabaseTest.php
@@ -303,6 +303,20 @@ public function testNestedQueryConditions() {
303 303 $this->assertEqual($sql, $this->db->renderCommand($query));
304 304 }
305 305
  306 + public function testCastingQueryConditionsWithSchemaWithAlias() {
  307 + $query = new Query(array(
  308 + 'type' => 'read',
  309 + 'model' => $this->_model,
  310 + 'conditions' => array(
  311 + 'MockDatabasePost.title' => '007'
  312 + )
  313 + ));
  314 + $result = $this->db->renderCommand($query);
  315 +
  316 + $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE {MockDatabasePost}.{title} = '007';";
  317 + $this->assertEqual($sql, $result);
  318 + }
  319 +
306 320 public function testJoin() {
307 321 $query = new Query(array(
308 322 'type' => 'read',
@@ -355,6 +369,30 @@ public function testCreate() {
355 369 $this->assertEqual($expected, $result);
356 370 }
357 371
  372 + public function testCreateWithValueBySchema() {
  373 + $entity = new Record(array(
  374 + 'model' => $this->_model,
  375 + 'data' => array('title' => '007', 'body' => 'the body')
  376 + ));
  377 + $query = new Query(compact('entity') + array(
  378 + 'type' => 'create',
  379 + 'model' => $this->_model
  380 + ));
  381 + $hash = $query->export($this->db);
  382 + ksort($hash);
  383 + $expected = sha1(serialize($hash));
  384 +
  385 + $result = $this->db->create($query);
  386 + $this->assertTrue($result);
  387 + $result = $query->entity()->id;
  388 + $this->assertEqual($expected, $result);
  389 +
  390 + $expected = "INSERT INTO {mock_database_posts} ({title}, {body})";
  391 + $expected .= " VALUES ('007', 'the body');";
  392 + $result = $this->db->sql;
  393 + $this->assertEqual($expected, $result);
  394 + }
  395 +
358 396 public function testCreateWithKey() {
359 397 $entity = new Record(array(
360 398 'model' => $this->_model,
@@ -461,6 +499,23 @@ public function testUpdate() {
461 499 $this->assertEqual($sql, $this->db->renderCommand($query));
462 500 }
463 501
  502 + public function testUpdateWithValueBySchema() {
  503 + $entity = new Record(array(
  504 + 'model' => $this->_model,
  505 + 'data' => array('id' => 1, 'title' => '007', 'body' => 'the body'),
  506 + 'exists' => true
  507 + ));
  508 + $query = new Query(compact('entity') + array('type' => 'update'));
  509 + $result = $this->db->update($query);
  510 +
  511 + $this->assertTrue($result);
  512 + $this->assertEqual(1, $query->entity()->id);
  513 +
  514 + $expected = "UPDATE {mock_database_posts} SET";
  515 + $expected .= " {id} = 1, {title} = '007', {body} = 'the body' WHERE {id} = 1;";
  516 + $this->assertEqual($expected, $this->db->sql);
  517 + }
  518 +
464 519 public function testDelete() {
465 520 $entity = new Record(array(
466 521 'model' => $this->_model,
@@ -657,7 +712,7 @@ public function testConditions() {
657 712 'type' => 'read', 'model' => $this->_model,
658 713 'conditions' => array('lower(title)' => 'test')
659 714 ));
660   -
  715 +
661 716 $this->assertEqual($sql, $this->db->renderCommand($query));
662 717
663 718 $query = new Query(array(

0 comments on commit 884e839

Please sign in to comment.
Something went wrong with that request. Please try again.