WebアプリケーションにZendFramework1.10を使用しており、cronで使用されるcli phpのスクリプトにZend_Db_Adapter(およびおそらく私のモデルの一部)を使用したいと思います。application.iniの設定に基づいてZend_Db_Adapterを作成する方法を教えてください。Zend_Db_Adapterが必要なときのアプリケーションのモデルでは、次のようなものをどこでも使用しました。
$this->_db = Zend_Db_Table::getDefaultAdapter();
CLIにも使用するとよいでしょう。たとえば、ファイルcron_jobを作成しましたが、ここでは$resourceがnullです。set_include_path(implode(PATH_SEPARATOR、array($ zendPath、get_include_path()、)));
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH',
/* realpath(dirname(__FILE__) . '/../application')*/
'/var/application');
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
// Register autoloader
require_once($zendPath . '/Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance();
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$resource = $application->getBootstrap()->getPluginResource('db');
var_dump($resource);
application.iniには、次の設定があります。
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = user
resources.db.params.password = password
resources.db.params.unix_socket = "/var/run/mysqld/mysqld.sock"
resources.db.params.dbname = db_name
resources.db.isDefaultTableAdapter = true
ここで、CLIスクリプトの別の構成を作成しないようにアプリケーションのapplication.iniに基づいてZend_Db_Adapterを作成するための最良の方法は何ですか?Webアプリケーションのブートストラップは次のようなものです。
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/**
* Initilize Action helper in a broker
*/
protected function _initActionHelpers()
{
Zend_Controller_Action_HelperBroker::addPrefix('Common_Helper');
}
/**
* Init plugins
*/
protected function _initPlugins()
{
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_AccessCheck());
}
/**
* Set transport for mail
*/
protected function _initEmail()
{
//parameters to send mail
$emailConfig = $this->getOption('email');
//send parameters for sending email
$transport = new Zend_Mail_Transport_Smtp($emailConfig['server'],
$emailConfig);
Zend_Mail::setDefaultTransport($transport);
//object for sending mail
$mailer = new Common_Mail();
$mailer->setFrom($emailConfig['from_address'], $emailConfig['from_name']);
//congigure and store mailer for sending email
Zend_Registry::set('mailer', $mailer);
}
}
ありがとうございました。