3

I have two databases, (MySQL and Oracle), I did the connection betweek sf2 and both databases, here is my config.yml file:

doctrine:
dbal:
    default_connection:   default
    connections:
        default:
            driver:   "%database_driver%"
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
            # if using pdo_sqlite as your database driver, add the path in parameters.yml
            # e.g. database_path: "%kernel.root_dir%/data/data.db3"
            # path:     "%database_path%"
        sysman:
            driver:   %database_driver2%
            host:     %database_host2%
            port:     %database_port2%
            dbname:   %database_name2%
            user:     %database_user2%
            password: %database_password2%
            charset:  UTF8

My question is, how can I run console command on the second database (Oracle), commands like (doctrine:database:create ...), and thanks

user3303133
  • 105
  • 1
  • 10

2 Answers2

3

Use the --connection parameter:

php app/console doctrine:database:create --connection=default

or

php app/console doctrine:database:create --connection=sysman
j0k
  • 22,600
  • 28
  • 79
  • 90
  • Thanks for the answer, when I use it for the sysman connection I have this error: PHP Notice: Use of undefined constant OCI_COMMIT_ON_SUCCESS - assumed 'OCI_COMMIT_ON_SUCCESS' in D:\Sites\odd\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\OCI8\Driver.php on line 37 – user3303133 May 05 '14 at 18:00
  • Well, this is an other problem which you should cover in an other question and first of all, make a quick search on Google and you'll find this question: http://stackoverflow.com/q/9877592/569101 – j0k May 05 '14 at 18:03
  • Well thanks, I'll see this, one last point, I did check for documentation how to use oracle with sf2 (CRUD) I didn't find anything, is it the same as MySQL? – user3303133 May 05 '14 at 18:05
  • I guess that if Doctrine handle Oracle the way it handles MySQL, every thing should be the same :) – j0k May 05 '14 at 18:06
  • So, I can use the same stuffs like repositories ... ? something like: $this->getDoctrine()->getManager()->getRepository('AcmeSiteBundle:Article')->findArticle($id); – user3303133 May 05 '14 at 18:08
0

You should first read a tutorial about commands and how to pass options and parameters to the commands. And how to distinguish between an option and a parameter.

If you want to make your own commands...

You will probably want to make it like this - if you do not pass an option (you will use the default database), if you pass it, you will make sure it is a valid option, and use the passed database connection name.

Doctrine is not tightly coupled with Mysql, you can use almost all most common available databases.

Also note, commands are container aware. That means you commands can access container, though which you have access to your services, such as doctrine:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $connection $input->getArgument('connection');
    # Validate connection argument here and use it below

    $container = $this->getContainer();
    $em = $container->get('doctrine')->getManager(); // default db
    $em = $container->get('doctrine')->getManager('sysman'); // another

    return 1;
}

I wrote the code without testing, excuse me for any mistake I might have done.

php app/console doctrine:mapping:info --em=default (same without em option)
php app/console doctrine:mapping:info --em=sysman
tomazahlin
  • 2,137
  • 1
  • 24
  • 29
  • Thanks for the code I think it will work logically, where should I put this ? I think in doctrine it's already done, because when I try the solution proposed by @j0k it pass and gives me another error – user3303133 May 06 '14 at 10:24
  • Use this code only, if you write your own commands, do not alter the commands provided by doctrine. For existing doctrine commands, use something like I updated above. – tomazahlin May 06 '14 at 10:52
  • yes sure, I still have a problem, I can't connect to oracle using doctrine, this $this->get('doctrine')->getManager('sysman')->getConnection()->isConnected() return false !! – user3303133 May 06 '14 at 11:02
  • I've add more details here http://stackoverflow.com/questions/23493009/connect-oracle-to-symfony2 – user3303133 May 06 '14 at 11:09