脚本根据 application.ini 中的设置为 cli php 创建 Zend_Db_Adapter
我使用 Zend Framework 1.10 作为 Web 应用程序,并且我想使用 Zend_Db_Adapter (也许还有我的一些模型)作为 cli php 中的脚本,该脚本将与 cron 一起使用。 您能告诉我如何根据application.ini中的设置创建Zend_Db_Adapter吗? 在应用程序模型中,当我需要 Zend_Db_Adapter 时,我到处使用类似这样的东西:
$this->_db = Zend_Db_Table::getDefaultAdapter();
It would be good to use it for cli too。 例如,我创建了一个文件 cron_job 并且 $resource 在这里为空。 set_include_path(内爆(PATH_SEPARATOR,数组( $zendPath, 获取包含路径(), )));
// 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
What is the best way to make create Zend_Db_Adapter here based on application.ini of my application 不为 cli 脚本创建另一个配置? 我的网络应用程序引导程序是这样的:
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);
}
}
谢谢。
I use Zend Framework 1.10 for web application and I'd like to use Zend_Db_Adapter (and maybe some of my models) for script in cli php that will be used with cron.
Could you please tell how to create Zend_Db_Adapter based on settings in application.ini?
In the model of application when I needed Zend_Db_Adapter I used everywhere something like this:
$this->_db = Zend_Db_Table::getDefaultAdapter();
It would be good to use it for cli too.
For example I created a file cron_job and $resource is null here.
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);
In application.ini I have settings:
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
What is the best way to make create Zend_Db_Adapter here base on application.ini of my application not to create another configuration for cli scripts?
my bootstrap for web application is something like that:
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);
}
}
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的脚本没有给您返回数据库,因为您需要首先引导数据库。
试试这个:
$application->getBootStrap()->bootstrap('db);
然后执行
$resource = $application->getBootstrap()->getPluginResource('db');
更一般地说:
** 您可以在同一个 Bootstrap 文件中执行 Web 应用程序和 cli 操作,这样,您就可以以类似的方式对两者进行引导。这意味着您可以将数据库设置的处理转移到 Bootstrap.php 文件中,并将该 Bootstrap 文件用于您的 Web 应用程序和您执行的任何 cli 操作。您可以使用引导程序机制的依赖机制以多种灵活的方式进行引导,例如,一种用于 Web,一种用于 cli。**
在您的引导程序中添加以下内容:
扩展讨论:
假设您有这样的 Bootstrap.php 文件
WHERE _initTheDb 和 _initZ 是您想要在 Web 和 cli 中执行的操作,而 _initX 是您不想在 cli 中执行的操作。
为 cli 创建一个额外的 _init:
现在在您的 cli 加载器脚本中,只需确保您为 cli 进行引导即可。 (这将引导 db 部分以及您添加到 _initCli 的任何其他部分)。
问题:既然 _initCli 现在存在于您的 Bootstrap 中,如何防止 Web 引导进程同时引导“cli”部分?
X 仅由 Web 应用程序引导(位于 Bootstrap 文件的顶部)。
如果已引导,则必须在 Web 环境中执行。
使用这些知识可以跳过引导 cli 部分:
I think your script is not giving you back a db, because you need to first bootstrap the db.
Try this:
$application->getBootStrap()->bootstrap('db);
THEN do
$resource = $application->getBootstrap()->getPluginResource('db');
On a more general note:
** you can do both web application stuff and cli stuff off of the same Bootstrap file, that way, you can have things bootstrapped in similar ways for both. This means that you would move dealing with the db setup to your Bootstrap.php file and use that Bootstrap file for both your web application and any cli stuff you do. You can use the dependency mechanism of the bootstrap mechanism to bootstrap in multiple, flexible ways, say, one for web, one for cli.**
In your bootstrap add this:
Extended discussion:
Let's say you have Bootstrap.php file like this
WHERE _initTheDb and _initZ are things you want to do both in web and cli, and _initX is something you don't want to do in cli.
create an additional _init for cli:
Now in your cli loader script, just make sure you boot-strap for the cli. (This will bootstrap the db section and whatever other sections you've added to _initCli).
Question: How do you prevent the web bootstrap process from also bootstrapping the 'cli' section, since _initCli is now present in your Bootstrap?
X only gets boot-strapped by the web application (at the top of the Bootstrap file).
If it has been bootstrapped, you must be executing in the web environment.
Use this knowledge to skip bootstrapping the cli section:
看起来确实有效。我创建了 init.php,计划将其包含在将用于 cron 的所有脚本中。
看起来包含这个文件后我可以使用这样的东西:
看起来没有必要在 application.ini 中设置
我认为在 Zend Framework 的代码中默认设置为 true。
It looks like it works. I created init.php that I plan to inlcude in all scripts that will be used for cron.
It looks like after including of this file I can use something like this:
It looks like it's not necessary to in application.ini to set
I think this is set true by default in the code of Zend Framework.