Zend Frameworkでデータベース設定をどう記述するか
Zend Frameworkを使っていて困るのはその自由度。PHPで自作Framework(笑)を作っていた身としては嬉しいのですけど、カスタマイズし放題ではZend Frameworkを使う意味はあまりないですし、なるべくなら正しい方法を取りたいものです。
設定も自由に書けます。いちおう気にして書いていたつもりでしたが、もっといい方法を発見したので共有。
Example #2 Sample DB adapter resource configuration
Below is an example INI configuration that can be used to initialize the DB resource.
[production] resources.db.adapter = "pdo_mysql" resources.db.params.host = "localhost" resources.db.params.username = "webuser" resources.db.params.password = "XXXXXXX" resources.db.params.dbname = "test" resources.db.isDefaultTableAdapter = true
として、"Bootstrap.php" で
<?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initDb() { $resource = $this->getPluginResource('db'); $db = Zend_Db::factory($resource->getAdapter(), $resource->getParams()); Zend_Registry::set('database', $db); Zend_Db_Table::setDefaultAdapter($db); } }
と書いて、Bootstrap::getPluginResource() を使ってresourceを取る。
The following configuration keys are recognized: *adapter: Zend_Db adapter type. *params: associative array of configuration parameters to use when retrieving the adapter instance. *isDefaultTableAdapter: whether or not to establish this adapter as the default table adapter.
なんて書かれているので、これからはこの方式でやっていこうかとオモッテますよ。
結構きれいですね。