In Yii2 How to insert data from one table to another table.
Here i have two tables table1
and table2
.
Now what i need is when a condition met i need transfer a specific data from table1
to table2
.
so help to write insert Query for this scenario in Yii2
This is the insert query given in yii2 docs
Yii::$app->db->createCommand()
->insert('user', [
'name' => 'Sam',
'age' => 30,
])->execute();
But i need this Query to be convert according Yii2 Query
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers;
QueryBuilder
's insert
method returns this:
return 'INSERT INTO ' . $schema->quoteTableName($table)
. ' (' . implode(', ', $names) . ') VALUES ('
. implode(', ', $placeholders) . ')';
So there is no way to specify SELECT
here.
Can't find it in core, I thinks it's not implemented because it's pretty rare case.
You can use your custom SQL code like this:
$sql = '...';
\Yii::$app->db->createCommand($sql)->execute();
Useful links:
P.S. I also reported issue here, so maybe it will be added to the core in the future. If you want it now for repeated usage, you can implement such method by yourself.