File tree Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -149,6 +149,9 @@ public function sql(ValueBinder $generator) {
149
149
foreach ($ this ->_values as $ row ) {
150
150
$ row = array_merge ($ defaults , $ row );
151
151
foreach ($ row as $ column => $ value ) {
152
+ if ($ value instanceof ExpressionInterface) {
153
+ $ value = $ value ->sql ($ generator );
154
+ }
152
155
$ type = $ this ->typeMap ()->type ($ column );
153
156
$ generator ->bind ($ i ++, $ value , $ type );
154
157
}
Original file line number Diff line number Diff line change @@ -2294,6 +2294,27 @@ public function testInsertFailureMixingTypesQueryFirst() {
2294
2294
->values (['name ' => 'mark ' ]);
2295
2295
}
2296
2296
2297
+ /**
2298
+ * Test that insert can use expression objects as values.
2299
+ *
2300
+ * @return void
2301
+ */
2302
+ public function testInsertExpressionValues () {
2303
+ $ query = new Query ($ this ->connection );
2304
+ $ query ->insert (['title ' ])
2305
+ ->into ('articles ' )
2306
+ ->values (['title ' => $ query ->newExpr ("SELECT 'jose' " )]);
2307
+
2308
+ $ result = $ query ->sql ();
2309
+ $ this ->assertQuotedQuery (
2310
+ "INSERT INTO <articles> \(<title>\) VALUES (?) " ,
2311
+ $ result ,
2312
+ true
2313
+ );
2314
+ $ result = $ query ->execute ();
2315
+ $ this ->assertCount (1 , $ result );
2316
+ }
2317
+
2297
2318
/**
2298
2319
* Tests that functions are correctly transformed and their parameters are bound
2299
2320
*
Original file line number Diff line number Diff line change @@ -292,6 +292,19 @@ public function testSaveWithCallbacks() {
292
292
$ this ->assertSame ($ article , $ articles ->save ($ article ));
293
293
}
294
294
295
+ /**
296
+ * Test that save() works with entities containing expressions
297
+ * as properties.
298
+ *
299
+ * @return void
300
+ */
301
+ public function testSaveWithExpressionProperty () {
302
+ $ articles = TableRegistry::get ('Articles ' );
303
+ $ article = $ articles ->newEntity ();
304
+ $ article ->title = new \Cake \Database \Expression \QueryExpression ("SELECT 'jose' " );
305
+ $ this ->assertSame ($ article , $ articles ->save ($ article ));
306
+ }
307
+
295
308
/**
296
309
* Tests that whe saving deep associations for a belongsToMany property,
297
310
* data is not removed becuase of excesive associations filtering.
You can’t perform that action at this time.
0 commit comments