@@ -3368,8 +3368,9 @@ public function enableTableKeys($tableName, $schemaName = null)
3368
3368
*/
3369
3369
public function insertFromSelect (Select $ select , $ table , array $ fields = [], $ mode = false )
3370
3370
{
3371
- $ query = 'INSERT ' ;
3372
- if ($ mode == self ::INSERT_IGNORE ) {
3371
+ $ query = $ mode === self ::REPLACE ? 'REPLACE ' : 'INSERT ' ;
3372
+
3373
+ if ($ mode === self ::INSERT_IGNORE ) {
3373
3374
$ query .= ' IGNORE ' ;
3374
3375
}
3375
3376
$ query = sprintf ('%s INTO %s ' , $ query , $ this ->quoteIdentifier ($ table ));
@@ -3380,26 +3381,36 @@ public function insertFromSelect(Select $select, $table, array $fields = [], $mo
3380
3381
3381
3382
$ query = sprintf ('%s %s ' , $ query , $ select ->assemble ());
3382
3383
3383
- if ($ mode == self ::INSERT_ON_DUPLICATE ) {
3384
- if (!$ fields ) {
3385
- $ describe = $ this ->describeTable ($ table );
3386
- foreach ($ describe as $ column ) {
3387
- if ($ column ['PRIMARY ' ] === false ) {
3388
- $ fields [] = $ column ['COLUMN_NAME ' ];
3389
- }
3390
- }
3391
- }
3392
- $ update = [];
3393
- foreach ($ fields as $ field ) {
3394
- $ update [] = sprintf ('%1$s = VALUES(%1$s) ' , $ this ->quoteIdentifier ($ field ));
3395
- }
3384
+ if ($ mode === self ::INSERT_ON_DUPLICATE ) {
3385
+ $ query .= $ this ->renderOnDuplicate ($ table , $ fields );
3386
+ }
3396
3387
3397
- if ($ update ) {
3398
- $ query = sprintf ('%s ON DUPLICATE KEY UPDATE %s ' , $ query , join (', ' , $ update ));
3388
+ return $ query ;
3389
+ }
3390
+
3391
+ /**
3392
+ * Render On Duplicate query part
3393
+ *
3394
+ * @param string $table
3395
+ * @param array $fields
3396
+ * @return string
3397
+ */
3398
+ private function renderOnDuplicate ($ table , array $ fields )
3399
+ {
3400
+ if (!$ fields ) {
3401
+ $ describe = $ this ->describeTable ($ table );
3402
+ foreach ($ describe as $ column ) {
3403
+ if ($ column ['PRIMARY ' ] === false ) {
3404
+ $ fields [] = $ column ['COLUMN_NAME ' ];
3405
+ }
3399
3406
}
3400
3407
}
3408
+ $ update = [];
3409
+ foreach ($ fields as $ field ) {
3410
+ $ update [] = sprintf ('%1$s = VALUES(%1$s) ' , $ this ->quoteIdentifier ($ field ));
3411
+ }
3401
3412
3402
- return $ query ;
3413
+ return count ( $ update ) ? ' ON DUPLICATE KEY UPDATE ' . join ( ' , ' , $ update ) : '' ;
3403
3414
}
3404
3415
3405
3416
/**
0 commit comments