Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a doctrine connection to import a SQL file?

Tags:

doctrine-orm

I have an app that needs to import a .sql file. I can import the file from the command line with mysql -u my_user -pMyPassword db_name < import.sql, but I'd like to move this into my app. I have some things that need to be done before the import and others after. Right now I have to break it into 3 steps. The closest to a solution I've found was to get the connection (Doctrine\DBAL\Connection) and use exec() but it throws syntax errors even though my source file is correct. I'm guessing it's trying to escape things and double escaping the SQL. The file was generated with mysqldump.

like image 802
Asa Ayers Avatar asked Sep 07 '12 18:09

Asa Ayers


2 Answers

With Symfony using Doctrine, you can do it with:

php app/dev_console doctrine:database:import import.sql
like image 132
Dertron Avatar answered Sep 19 '22 20:09

Dertron


You can use the DBAL "import" command and get the sql executed. This is anyway less performant than using the mysql command directly, since it loads the entire file into memory.

Otherwise, I'd suggest you to use your own Symfony Console Command.

like image 42
Ocramius Avatar answered Sep 19 '22 20:09

Ocramius