2

For some reasons I don't want my users to edit /app/config/config.php to add the database credentials, but in a separate file they add them as an array and my /app/config/config.php is PHP, not YAML nor XML, so how can I tell /app/config/config.php to get database credentials as an array from another file?

I did look at an application that works like that and I see they use:

$container->loadFromExtension('doctrine', array(
 'orm' => array(
  'auto_generate_proxy_classes' => false,
  'default_entity_manager' => 'default',
  'entity_managers' => array(
   'default' => array('mappings' => array('MyApp' => array('type' => 'staticphp')), 'class_metadata_factory_name' => 'Doctrine\\ORM\\Mapping\\StaticClassMetadataFactory')
  )
 ),
 'dbal' => array(
  'default_connection' => 'default',
  'connections' => array(
   'default' => array('host' => 'from_user_config.db', 'logging' => true),
   'read' => array('host' => 'from_user_config.db_read', 'logging' => true)
  )
 )
));

Since it is not documented, I appreciate if you clarify how can I do this the way I described?

Makyen
  • 31,849
  • 12
  • 86
  • 121
user4271704
  • 723
  • 1
  • 12
  • 37
  • 1
    There is something about it in the docs (http://symfony.com/doc/current/cookbook/configuration/external_parameters.html#miscellaneous-configuration). – qooplmao Jan 02 '15 at 10:01
  • Thanks. Because of some reason setParameter is not good for my application. If I create a normal doctrine bootstrap file and use import() that file in symfony, does symfony recognize the doctrine bootstrap file this way? I don't want my user edit /app/config/config.php nor parameters. They should only edit a separate config file with db credentials, and I want to inject that config file into symfony /app/config/config.php file as well as some doctrine settings such as proxy, cache, tablePrefix eventListener etc. How is the possible with symcony? – user4271704 Jan 03 '15 at 18:22

1 Answers1

1

Though it's explained in the article provided by Qoop, I must admit that the explanation is a bit vague so I'm going to elaborate in case it helps you.

First off, in your app/config/config.php include the following line.

// app/config/config.php
$loader->import('my_custom_parameters.php');

create the file my_custom_parameters.php (documentation's example, I'll provide mine later on).

// app/config/my_custom_parameters.php
include_once('/path/to/drupal/sites/default/settings.php');
$container->setParameter('drupal.database.url', $db_url);

In the documentation's example there's another import in case you want your settings to be stored somewhere else. It's not mandatory to do so and you can include the variables directly in parameters.php if you like.

For simplicity, I'm going to get rid of the include_once and I'm going to modify the credentials directly.

// app/config/my_custom_parameters.php
$container->setParameter('database_name', 'the_db_I_want');
$container->setParameter('database_user', 'the_user_I_want');
$container->setParameter('database_password', 'the_pass_I_want');

As you can see, you can override as many parameters as you want.

UPDATE

You can also try this (haven't tested but think will work):

// app/config/my_custom_parameters.php
include_once('/path/to/where/people/can/modify/settings.php');
$container->loadFromExtension('doctrine', array(
 'orm' => array(
  'auto_generate_proxy_classes' => $variable_from_settings_php,
  'default_entity_manager' => $another_variable_from_settins_php,
  'entity_managers' => array(
   'default' => array('mappings' => array('MyApp' => array('type' => 'staticphp')), 'class_metadata_factory_name' => 'Doctrine\\ORM\\Mapping\\StaticClassMetadataFactory')
  )
 ),
 'dbal' => array(
  'default_connection' => 'default',
  'connections' => array(
   'default' => array('host' => 'from_user_config.db', 'logging' => true),
   'read' => array('host' => 'from_user_config.db_read', 'logging' => true)
  )
 )

I'm using the code you provided. You can use the configuration you like. I've added some dummy variables that could be loaded from the file imported as parameter values just to show you that, in theory, you could do something similar.

As you can read in the documentation (the part talking about setting up the bd, 'Configuring the Database' is called):

Defining the configuration via parameters.yml is just a convention. The parameters defined in that file are referenced by the main configuration file when setting up Doctrine

Give it a try and let me know if it works.

Hope it's clearer.

acontell
  • 6,792
  • 1
  • 19
  • 32
  • Thanks. Because of some reason setParameter is not good for my application. If I create a normal doctrine bootstrap file and use import() that file in symfony, does symfony recognize the doctrine bootstrap file this way? I don't want my user edit /app/config/config.php nor parameters. They should only edit a separate config file with db credentials, and I want to inject that config file into symfony /app/config/config.php file as well as some doctrine settings such as proxy, cache, tablePrefix eventListener etc. How is the possible with symcony? – user4271704 Jan 03 '15 at 18:21
  • @user4271704 check the update and let me know if it works, never tried it and I'm curious about it (I usually use yml) – acontell Jan 03 '15 at 18:53
  • why not just doing this all directly in /app/config/config.php instead of parameters.php then include parameters.php in config.php? – user4271704 Jan 04 '15 at 07:13
  • @user4271704 as you can read in the documentation, the use of parameters.php is just a convention. It's up to you to do whatever you think is more suitable to your application, I'm just giving ideas. – acontell Jan 04 '15 at 13:20
  • Thanks. What about making doctrine bootstrap as a service and in /config/config.php calls the service? If good idea, how can I do this all? – user4271704 Jan 04 '15 at 13:37
  • @user4271704 Not really sure about that sorry. However, you could override the entity manager and use the one that gets created in your doctrine bootstrap file (or something like that). In this [stackoverflow post](http://stackoverflow.com/questions/8138962/is-there-a-way-to-specify-doctrine2-entitymanager-implementation-class-in-symfon) you'll find info about how to go about that. Good luck. – acontell Jan 04 '15 at 15:17
  • that does not help because it just override the default entity manager. $config and $evm and $conn still needs to be set in symfony parameters. Ny problem is that I want to inject them from another file where the user define credentials. How possible? – user4271704 Jan 06 '15 at 09:43
  • @user4271704 The only documented way to override parameters that I found was the one presented. If you found my answer helpful, please vote up, otherwise don't. – acontell Jan 06 '15 at 10:01
  • Of course useful answer. But it seems I don't have enough reputation to vote up. If you found my questions please vote up that I get some reputation! – user4271704 Jan 06 '15 at 16:29
  • I know DeskPro implemented a nice trick to have doctrine dbal/orm within symfony, but I don't understand their source code well, would you please look at it and explain how they did this? – user4271704 Jan 06 '15 at 17:06
  • I did that php config for dbal as stated on this question but I get doctrine extension is not registered exception, I deleted /app/cache folder but still the same problem. Please advice. – user4271704 Jan 06 '15 at 17:10
  • @user4271704 Hi there, sorry, I've run out of ideas, maybe someone else is able to offer you an alternative. Good luck – acontell Jan 06 '15 at 17:26