Skip to content

Commit

Permalink
Ensure that values are properly cast for INSERT and UPDATE queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
nealerickson committed Aug 24, 2012
1 parent 198523c commit 9e9945c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data/source/Database.php
Expand Up @@ -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()));

Expand Down
41 changes: 41 additions & 0 deletions tests/cases/data/source/DatabaseTest.php
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9e9945c

Please sign in to comment.