Skip to content

Commit

Permalink
Fix insert queries failing with expression objects.
Browse files Browse the repository at this point in the history
Insert queries should support expression objects as values. This allows
geo features from MySQL to be used, and enables other platform specific
SQL functions.

Refs #4671
  • Loading branch information
markstory committed Sep 21, 2014
1 parent b88a0d8 commit bbbe6a9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Database/Expression/ValuesExpression.php
Expand Up @@ -149,6 +149,9 @@ public function sql(ValueBinder $generator) {
foreach ($this->_values as $row) {
$row = array_merge($defaults, $row);
foreach ($row as $column => $value) {
if ($value instanceof ExpressionInterface) {
$value = $value->sql($generator);
}
$type = $this->typeMap()->type($column);
$generator->bind($i++, $value, $type);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -2294,6 +2294,27 @@ public function testInsertFailureMixingTypesQueryFirst() {
->values(['name' => 'mark']);
}

/**
* Test that insert can use expression objects as values.
*
* @return void
*/
public function testInsertExpressionValues() {
$query = new Query($this->connection);
$query->insert(['title'])
->into('articles')
->values(['title' => $query->newExpr("SELECT 'jose'")]);

$result = $query->sql();
$this->assertQuotedQuery(
"INSERT INTO <articles> \(<title>\) VALUES (?)",
$result,
true
);
$result = $query->execute();
$this->assertCount(1, $result);
}

/**
* Tests that functions are correctly transformed and their parameters are bound
*
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -292,6 +292,19 @@ public function testSaveWithCallbacks() {
$this->assertSame($article, $articles->save($article));
}

/**
* Test that save() works with entities containing expressions
* as properties.
*
* @return void
*/
public function testSaveWithExpressionProperty() {
$articles = TableRegistry::get('Articles');
$article = $articles->newEntity();
$article->title = new \Cake\Database\Expression\QueryExpression("SELECT 'jose'");
$this->assertSame($article, $articles->save($article));
}

/**
* Tests that whe saving deep associations for a belongsToMany property,
* data is not removed becuase of excesive associations filtering.
Expand Down

0 comments on commit bbbe6a9

Please sign in to comment.