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

(see my changes below for a better question) . How can I control which connection is selected (from the right side of the environment in database.yml) in the symfony1.4 task, using only the generic Doctrine_Query::create() to create a query?

I am using database.yml, which 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 of these database definitions is used when calling:

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

At the moment, I cannot establish a connection like this: $query = Doctrine_Query::create($conn); .

Any help would be greatly appreciated!


EDIT:

I have a lot of code in software that uses Doctrine_Query::create() (without a connection argument). It seems to be choosing the right environment and connection through web requests. But I can’t understand how this is done, so I can make my CLI commands the same (they don’t choose the correct connection and environment at the moment). That's why I need to know how to control which connection is “automatically” used (selected by default).

Question:

So, I think, in conclusion, my question is:

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

+6
source share
3 answers

The cause of my actual problem

After some deep debugging using a clean task, I finally found my culprit. Symfony 1.4 seems to run the configure method for each task in the task directory before executing the task.

Unfortunately, some uninformed prankster (former colleague * ) included some initialization of the hard coded context in one of these methods, for example:

 // inside a task protected function configure() { // context was initialized $configuration = ProjectConfiguration::getApplicationConfiguration('app_name', 'prod', true); $context = sfContext::createInstance($configuration); // so the following was available in the configure method $save_path = sfConfig::get('sf_app_config_dir'); // rest of configure method implementation // ... } 

This got the database settings for all cronjobs in all other environments, except for the production environment (fortunately).

I managed to get around this by doing something like this:

 protected function configure() { $configuration = ProjectConfiguration::getApplicationConfiguration('app_name', 'prod', true); // removed the context creation $configuration->activate(); // this makes the sfConfig::get() work $save_path = sfConfig::get('sf_app_config_dir'); // rest of configure method implementation // ... } 

Answer my question

I also found that when it comes to controlling which database connection Doctrine_Query::create() is using, you can have some control using something like this for a higher level function:

 // for making sure the 'auth' database settings are used as a default Doctrine_Manager::getInstance()->setCurrentConnection('auth'); 

However, this has no authority over the fact that the "environment partition" is used to select the correct / dsn configuration for the database. This is done using the following:

 // somewhere in the execute method $env = 'dev'; // the environment section name; $configuration = ProjectConfiguration::getApplicationConfiguration('app_name', $env, true); $context = sfContext::createInstance($configuration); 

As also correctly outlined in the answers by Alex Blex and Marek . Using the task option to support multiple environments, as they suggest, makes sense.

*: My former colleague whom I can hardly go crazy because of the uniqueness and counteraction to the intuitive nature of this problem;)

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

must work. Depending on where you select Product , the parameter may be "doctrine", "journal" or "auth".

Could you explain why you are "unable to establish a connection" in this way?

EDIT:

So, if I'm right, you need to specify the environment in the cli command in order to use the dsn connection string in the right section in database.yml . You can use env for your cli commands. Maybe you need to add something like

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

to configure the task.

+4
source

By default, a task is generated using this code:

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

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

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

This will create a context using the task application option. You probably want to set the default value for it, change the configure() method:

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

Notice that I added frontend to initialize the frontend application. You can also configure the default setting for env.

+2
source

All Articles