1

I know I can execute (actually, creating it, not yet executed) custom SQL in Yii using:

$connection=Yii::app()->db;   // assuming you have configured a "db" connection
// If not, you may explicitly create a connection:
// $connection=new CDbConnection($dsn,$username,$password);
$command=$connection->createCommand($sql);
// if needed, the SQL statement may be updated as follows:
// $command->text=$newSQL;

Question: How can I execute a sql dump? i.e.

`$dump = file_get_contents('products.sql');`

Note: This dump has multiple commands - AFAIK createCommand works for single command - Am I wrong?

Note 2: I cannot break queries by ';' character (SQL delimiter) since such character is widely used among product data, so I have to execute the dump.

1 Answer 1

4

You CAN execute multiple commands with createCommand()

MySQL:

CREATE TABLE tst (
  f varchar(255) DEFAULT NULL
)
ENGINE = INNODB
CHARACTER SET utf8
COLLATE utf8_general_ci;

PHP:

...
Yii::app()->db->createCommand("INSERT INTO tst SET f='test1'; INSERT INTO tst SET f='test2';")->execute();
...

MySQL:

mysql> select * from tst;
+-------+
| f     |
+-------+
| test1 |
| test2 |
+-------+
2 rows in set (0.00 sec)

So if your products.sql contains valid SQL this should work:

$sql = file_get_contents('products.sql');
Yii::app()->db->createCommand($sql)->execute();
Sign up to request clarification or add additional context in comments.

2 Comments

:D tested it. when posted the question i was seeking for a good approach since I did not reach that part yet.
ALTHOUGHT actually, for 1.1.14, the line would be: Yii::app()->db->createCommand($sql)->execute();. I edited like that to make it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.