mysql - Symfony 2 Console command for creating custom Database -
I am working on the Simplon 2 project where each user has his database. In my config.yml file, The principle is: dbal: orm is set to a client, but there is no connection property because they are set to runtime and are referenced by all users. I.e I only have a default debil connection and two orm-connection and the user's volume is unlimited.
This works fine but I need to create databases and schemas when user is registered (FOS UserBundle). I can keep my logic in the Extended User Bundle Controller The problem is that I can not run 'php app / console doctrine: database: create' because there are no fixed parameters for the new user.
Is there a way to specify custom database parameters for the console commands? I could probably find some pretty ugly mysql commands around this but I'd rather not want to thank many in advance!
You can create your own order as outline using the code given below: < / P> Namespot theory / bundle \ datrein bundle \ command; Use Symfony \ Component \ Console \ Input \ InputOption; Use Symfony \ Component \ Console \ Input \ InputInterface; Use Symfony \ Component \ Console \ Output \ OutputInterface; Use the principle \ DBAL \ DriverManager; Classroom CreateDatabaseDoctrineCommandDynamically Extended Doctrine Commands {Configure Secure Function () {$ this - & gt; SetName ('Principle: Database: Created Dynamic') - & gt; Set descriptio ('creates configured database'); } / ** * {@ Inherit Doc} * / Perform the protected function (input input $ input, output input $ output) {/ *** ** To get the database configuration, edit this part below, though You want ** / $ connectionFactory = $ this - & gt; Container- & gt; Get ('doctrine.dbal.connection_factory'); $ Connection = $ connectionFactory-> CreateConnection ('driver' = & gt; 'pdo_mysql', 'user' = & gt; 'root', 'password' = & gt; '', 'host' = & gt; 'localhost', 'dbname' = & Gt; 'foo_database',)); $ Params = $ connection-> GetParams (); $ Name = isset ($ params ['path'])? $ Params ['Path']: $ params ['dbname']; Not set ($ parameter ['DBNAME']); $ TmpConnection = Driver Manager: Match Connection ($ Parameter); // Quotation only if we have no way, (If! (Issat ($ params ['path'])) {$ name = $ tmpConnection-> GetDatabasePlatform () - & gt; Bid Sending Identifier ($ name );} $ Error = false; {$ TmpConnection-> GetSchemaManager () -> Try Database ($ name); $ output- & gt; written (sprintf ('& lt; info & gt; The database was created for connection & lt; comment & gt;% s & lt; / comment & gt; & lt; / info & gt; ', $ name);} hold (\ exception $ e) {$ output- & Gt; written (sprintf ('& lt; error & gt; database for named connection & lt; comment & gt;% s & lt; / comment & gt; & Lt; / error & gt; ', $ name)); $ output- & gt; written (sprintf (' & lt; error & gt;% s & lt; / error & gt; ', $ e- & gt; ; GetMessage ()); $ Error = true;} $ TmpConnection- & gt; off (); return error $? 1: 0;}}
Comments
Post a Comment