dshdb64088 2011-02-21 16:17
浏览 63
已采纳

脚本根据cli php的application.ini中的设置创建Zend_Db_Adapter

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.

  • 写回答

2条回答 默认 最新

  • 普通网友 2011-02-22 16:09
    关注

    It looks like it works. I created init.php that I plan to inlcude in all scripts that will be used for cron.

    <?php
    // Define path to application directory
    defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', '/var/application');
    
    // Define application environment
    defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
    
    
    /** Zend_Application */
    require_once 'Zend/Application.php';
    
    // Create application, bootstrap, and run
    $application = new Zend_Application(
        APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini'
    );
    $application->getBootStrap()->bootstrap('db');
    

    It looks like after including of this file I can use something like this:

    $db = Zend_Db_Table::getDefaultAdapter();
    

    It looks like it's not necessary to in application.ini to set

    resources.db.isDefaultTableAdapter = true
    

    I think this is set true by default in the code of Zend Framework.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解