Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control which connection is selected from the database.yml in a symfony1.4 task

(See my edit below for a better question) How do I control which connection is selected (from the right environment section in the database.yml) in a symfony1.4 task using just a generic Doctrine_Query::create() to create the query?

I'm using a database.yml that looks something like this:

prod:
  doctrine:
    class: sfDoctrineDatabase
    param:
      dsn: mysql://some:pass@domain:port/database

  log:
    class: sfDoctrineDatabase
    param:
      dsn: mysql://some:pass@domain:port/database
  auth:
    class: sfDoctrineDatabase
    param:
      dsn: mysql://some:pass@domain:port/database

dev:
  doctrine:
    class: sfDoctrineDatabase
    param:
      dsn: mysql://some:otherpass@domain:port/database

  log:
    class: sfDoctrineDatabase
    param:
      dsn: mysql://some:otherpass@domain:port/database

  auth:
    class: sfDoctrineDatabase
    param:
      dsn: mysql://some:otherpass@domain:port/database

And I want to be able to control which one of those database definitions is used when calling something like:

$query = Doctrine_Query::create()
        ->select('*')
        ->from('Products p')
        ->where('p.id = ?', $productID)
        ->limit(1);

$OfpFlPr = $query->execute()->getFirst();

At the moment I'm not able to set the connection like so $query = Doctrine_Query::create($conn);.

Any help would be greatly appreciated!


EDIT:

I have lots of code deeper in the software where Doctrine_Query::create() is used (without the connection argument). It seems to select the right environment and connection through web requests. But I can't figure out how it does this, so I can make my CLI commands work the same way (they don't select the right connection and environment at the moment). That's why I need to know how to control which connection is 'automaticly' used (selected by default).

Question:

So I guess in conclusion my question would be:

How can I control which connection is selected by default in lower level code which uses Doctrine_Query::create() while the code is being executed as a symfony CLI command?

like image 397
Tommy Bravo Avatar asked Nov 03 '15 16:11

Tommy Bravo


2 Answers

$query = Doctrine_Query::create($doctrineManager->getConnection('doctrine'))
    ->select('*')
    ->from('Products p')
    ->where('p.id = ?', $productID)
    ->limit(1);

should work. Depending on where you select your Product from, the parameter may be 'doctrine', 'log', or 'auth'.

Could you elaborate why you are "not able to set the connection" this way?

EDIT:

So if I get it right, you like to specify environment in cli command, to use dsn connection string from the right section in database.yml. You can use env option for your cli commands to do so. You may need to add something like

$this->addOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'Specify environment', 'prod'); 

to the task configuration.

like image 122
Alex Blex Avatar answered Sep 19 '22 16:09

Alex Blex


By default, task is generated with this code:

$databaseManager = new sfDatabaseManager($this->configuration);
$connection = $databaseManager->getDatabase($options['connection'])->getConnection();

This initializes just the one connection, and does not initialize context. Instead, replace it with this:

sfContext::createInstance(new frontendConfiguration($options['env'], true));

This will create context, using application option of the task. You likely want to set default for it, change the task's configure() method to have:

    $this->addOptions(array(
        new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
        // ...
    ));

Notice I added frontend to initialize frontend app. You can also adjust the default for env option.

like image 25
Marek Avatar answered Sep 20 '22 16:09

Marek