2

I have these lines in my application.ini

how can I read user in my contrroler

resources.doctrine.dbal.connections.default.parameters.driver   = "pdo_mysql"
resources.doctrine.dbal.connections.default.parameters.dbname   = "zc"
resources.doctrine.dbal.connections.default.parameters.host = "localhost"
resources.doctrine.dbal.connections.default.parameters.port = 3306
resources.doctrine.dbal.connections.default.parameters.user = "root"
resources.doctrine.dbal.connections.default.parameters.password = "123456"

I use of this code but it retuens null

$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
 $user = $bootstrap->getOption('user');
var_dump($user);

edit: how may I read all of connections options ?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
ulduz114
  • 1,150
  • 6
  • 21
  • 37
  • Is it solved? And if so, could you please accept the answer that solved your problem. – wimvds Apr 19 '11 at 14:58
  • I think [this](http://framework.zend.com/manual/en/zend.config.adapters.ini.html) should help you. – S L Apr 18 '11 at 07:51

6 Answers6

6

I think you should use

$this->getInvokeArgs('bootstrap');

For more info see this chapter in manual.

What about using

$conf = $bootstrap->getOption('resources');
$dbConf = $conf['doctrine']['dbal']['connections']['default']['parameters'];
Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82
  • i think $bootstrap = $this->getInvokeArg('bootstrap'); and $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap'); are same – ulduz114 Apr 18 '11 at 08:37
  • Well I'm not sure. One more idea ;) – Tomáš Fejfar Apr 18 '11 at 14:08
  • But is there a way to get config params not like array elems but oop-style? Like `$a = $conf->one->two`?. Please my question about this http://stackoverflow.com/questions/12887873/how-to-retrieve-parameters-from-zend-application-ini-file-during-the-session – Green Oct 15 '12 at 15:40
  • `$config = new Zend_Config($conf);` or http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass – Tomáš Fejfar Oct 15 '12 at 17:32
3

How about something like:

$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
$connectionParams = $config->resources->doctrine->connections;

Or during Bootstrap, create and save this $config object in the Bootstrap or in the Zend_Registry for later retrieval in your controller.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • I think its not a good idea. You make system to read your application.ini twice. And then its uncomfortable to retrieve parameters from config with Zend_Registry. Please see my question on this http://stackoverflow.com/questions/12887873/how-to-retrieve-parameters-from-zend-application-ini-file-during-the-session – Green Oct 15 '12 at 15:36
  • Of course, you're right: he should not be reading/loading/parsing the .ini file twice. The important part is how to drill down into the config object/array to get at the data he needs. Thanks! ;-) – David Weinraub Oct 16 '12 at 18:12
1

This goes in your controller.

$bootstrap = $this->getInvokeArg('bootstrap');
$appinidata = $bootstrap->getOptions();
$user=$appinidata['resources']['doctrine']['dbal']['connections']['default']['parameters'] ['user'];

This should print "root".

print_r($user);
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
arielvtpma
  • 11
  • 1
0

In this case you should use Zend_Config_Ini class

$config = new Zend_Config_Ini('/path/to/config.ini','staging',$options);

second parameter is a section in INI file should be loaded ; third parameter is the key to allow modify loaded file.

You can put out value user this way:

$config->resources->doctrine->dbal->connections->default->parameters->user;
yuklia
  • 6,733
  • 5
  • 20
  • 26
0

you can set any variable using set method as following on index.php inside the public folder Let

$config = 'test'; Zend_Registry::set('config', $config);

once the variable has been set then you can get on any controllers/models by following method

Zend_Registry::get('config');

Hop it Helps!!

visit2shobhit
  • 183
  • 1
  • 4
  • 15
0

To get to the Doctrine container resource, just use :

$bootstrap = $this->getInvokeArg('bootstrap');
$doctrine = $bootstrap->getResource('doctrine');

From there you can drill down to the user name of the default connection (you can specify the connection if needed, just pass the name of the connection in the call to getConnection) :

$username = $doctrine->getConnection()->getUsername();
wimvds
  • 12,790
  • 2
  • 41
  • 42