From 9e9945c6aa086cd07de62b9c43894292906ba9fd Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Thu, 23 Aug 2012 22:31:20 -0400 Subject: [PATCH] Ensure that values are properly cast for INSERT and UPDATE queries. --- data/source/Database.php | 2 +- tests/cases/data/source/DatabaseTest.php | 41 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/data/source/Database.php b/data/source/Database.php index 21bc7efd70..b177e9bd97 100644 --- a/data/source/Database.php +++ b/data/source/Database.php @@ -782,7 +782,7 @@ public function _processConditions($key, $value, $context, $schema, $glue = 'AND */ public function fields($fields, $context) { $type = $context->type(); - $schema = (array) $context->schema(); + $schema = (array) $context->schema()->fields(); $modelNames = (array) $context->name(); $modelNames = array_merge($modelNames, array_keys((array) $context->relationships())); diff --git a/tests/cases/data/source/DatabaseTest.php b/tests/cases/data/source/DatabaseTest.php index 2d44ae767e..1214001a22 100644 --- a/tests/cases/data/source/DatabaseTest.php +++ b/tests/cases/data/source/DatabaseTest.php @@ -369,6 +369,30 @@ public function testCreate() { $this->assertEqual($expected, $result); } + public function testCreateWithValueBySchema() { + $entity = new Record(array( + 'model' => $this->_model, + 'data' => array('title' => '007', 'body' => 'the body') + )); + $query = new Query(compact('entity') + array( + 'type' => 'create', + 'model' => $this->_model + )); + $hash = $query->export($this->db); + ksort($hash); + $expected = sha1(serialize($hash)); + + $result = $this->db->create($query); + $this->assertTrue($result); + $result = $query->entity()->id; + $this->assertEqual($expected, $result); + + $expected = "INSERT INTO {mock_database_posts} ({title}, {body})"; + $expected .= " VALUES ('007', 'the body');"; + $result = $this->db->sql; + $this->assertEqual($expected, $result); + } + public function testCreateWithKey() { $entity = new Record(array( 'model' => $this->_model, @@ -475,6 +499,23 @@ public function testUpdate() { $this->assertEqual($sql, $this->db->renderCommand($query)); } + public function testUpdateWithValueBySchema() { + $entity = new Record(array( + 'model' => $this->_model, + 'data' => array('id' => 1, 'title' => '007', 'body' => 'the body'), + 'exists' => true + )); + $query = new Query(compact('entity') + array('type' => 'update')); + $result = $this->db->update($query); + + $this->assertTrue($result); + $this->assertEqual(1, $query->entity()->id); + + $expected = "UPDATE {mock_database_posts} SET"; + $expected .= " {id} = 1, {title} = '007', {body} = 'the body' WHERE {id} = 1;"; + $this->assertEqual($expected, $this->db->sql); + } + public function testDelete() { $entity = new Record(array( 'model' => $this->_model,