1

There are 2 databases used in Symfony2 project. I need to change second database details on the fly.

Is there any way to use container setParameter() in a function in controller of Symfony2?

Below is a piece of code where I need to change database name on the fly :

$container = $this->container;
$container->setParameter('second_database_host','localhost');
$container->setParameter('second_database_name','test');
$container->setParameter('second_database_user','root');
$container->setParameter('second_database_password','root');
$parameter_bag = $container->getParameterBag();
$service_manager = $container->get('projects_objects.manager');
$object_import = $service_manager->import($service_type));

But above code gives error as Impossible to call set() on a frozen ParameterBag.

Is there any other way from where I can change these parameters?

6
  • As the error says, is too late to change the conf. My question is: do you discover the db settings at runtime? Or the db is already defined? In the second case you can get the service from the container and instrument the service about which db need to use with a setter method. My two cents Commented Dec 10, 2015 at 7:31
  • @Matteo : I need to use the database setting at runtime. I cannot add it in parameter.yml. At one place where entitymanager was used there I created new database connection and with that database connection created a new entitymanager on the fly. But I don't know how I can do this for calling service. Commented Dec 10, 2015 at 7:37
  • have you already see this stackoverflow.com/questions/20805637/… ? Commented Dec 10, 2015 at 7:41
  • Yes by this way I am using entity managers but how to call service that I am not getting @Matteo Commented Dec 10, 2015 at 8:09
  • My idea is that you cold manually pass the new entityManager via a setter method to the service before call the import method. What you think about this? Commented Dec 10, 2015 at 9:03

1 Answer 1

0

if you discover the db coordinates at run-time i suggest to create an EntityManager on the fly and pass it to the service via a setter method. Your service can expose a setter on the $em class field for accept external EntityManager for do the stuff.

So in your controller you can instantiate the new EntityManager as described in this answer:

$conn = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'foo'
);

$new = \Doctrine\ORM\EntityManager::create(
    $conn,
    $em->getConfiguration(),
    $em->getEventManager()
);

Then pass it to the service as :

$service_manager = $container->get('projects_objects.manager');
$service_manager->setEntityManager($new); // <-- the entity manager istantiate on the fly
$object_import = $service_manager->import($service_type));  

The service so looks like:

....
// the entity manager
protected $em;

public function setEntityManager($em)
{
    $this->em=$em;
}

Hope this help

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.