Skip to content
Permalink
Browse files

Fix insert queries failing with expression objects.

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 bbbe6a9abe574113572727d5814e7c318cebabdc
@@ -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);
}
@@ -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
*
@@ -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.

0 comments on commit bbbe6a9

Please sign in to comment.
You can’t perform that action at this time.