脚本根据application.ini中cli php的设置创建Zend_Db_Adapter
我使用Zend Framework 1.10进行Web应用程序,我想使用Zend_Db_Adapter(也许我的一些模型)来编写将与cron一起使用的cli php中的脚本。
你能告诉我如何根据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
基于我的应用程序的application.ini创建Zend_Db_Adapter的最佳方法是什么,不要为cli脚本创建另一个配置?
我的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);
}
}
谢谢。
没有找到相关结果
已邀请:
2 个回复
盟犯涩沟都
扩展讨论: 假设您有这样的Bootstrap.php文件
_initTheDb和_initZ是你想要在web和cli中做的事情,_initX是你不想在cli中做的事情。 为cli创建一个额外的_init:
现在在你的cli loader脚本中,只需确保你为cli启动。 (这将引导db部分以及您添加到_initCli的其他任何部分)。
问题:如何阻止Web引导过程引导'cli'部分,因为_initCli现在存在于Bootstrap中? X只会被Web应用程序启动(在Bootstrap文件的顶部)。 如果已经引导,则必须在Web环境中执行。 使用此知识跳过cli部分的引导:
拟蓬
它看起来像包含这个文件后我可以使用这样的东西:
看起来没有必要在application.ini中进行设置
我认为这在Zend Framework的代码中默认设置为true。