0

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);          
    }


}

ありがとうございました。

4

2 に答える 2

0

最初にdbをブートストラップする必要があるため、スクリプトはdbを返していないと思います。

これを試してください:$ application-> getBootStrap()-> bootstrap('db);

その後、

$ resource = $ application-> getBootstrap()-> getPluginResource('db');


より一般的な注意事項:

**同じBootstrapファイルからWebアプリケーションとCLIの両方を実行できます。そうすれば、両方に対して同様の方法でブートストラップすることができます。これは、dbセットアップの処理をBootstrap.phpファイルに移動し、そのBootstrapファイルをWebアプリケーションと実行するCLIの両方に使用することを意味します。ブートストラップメカニズムの依存関係メカニズムを使用して、複数の柔軟な方法でブートストラップすることができます。たとえば、1つはWeb用、もう1つはCLI用です。**

ブートストラップにこれを追加します:

protected function _initTheDb
{
   //Following will automatically go to Zend_Application_Resource_Db,
   //if it can't find anything else named db to bootstrap
   $this->bootstrap('db');
   //do the above instead of the loader script 
   //which had: $resource = $application->getBootstrap()->getPluginResource('db'); 
   //Now setting up the db works for both cli and web,
   //if you add a _initCli() to your bootstrap.
}

拡張ディスカッション:

このようなBootstrap.phpファイルがあるとしましょう

protected function _initX
protected function _initTheDb
protected function _initZ

WHERE _initTheDbと_initZは、WebとCLIの両方で実行したいことであり、_initXは、CLIでは実行したくないことです。

cli用に追加の_initを作成します。

protected function _initCli()
{
    $this->bootstrap('TheDb');
    $this->bootstrap('Z');
    //BUT NOT $this->bootstrap('X'); since that that does apply for CLI.
}

ここで、CLIローダースクリプトで、CLIのブートストラップを確認してください。(これにより、dbセクションおよび_initCliに追加した他のセクションがブートストラップされます)。

$application->getBootStrap()->bootstrap('cli');

質問:_initCliがブートストラップに存在するようになったため、Webブートストラッププロセスが「cli」セクションもブートストラップしないようにするにはどうすればよいですか?

Xは、Webアプリケーション(Bootstrapファイルの先頭)によってのみブートストラップされます。ブートストラップされている場合は、Web環境で実行している必要があります。この知識を使用して、CLIセクションのブートストラップをスキップします。

protected function _initCli()
{
    if ($this->hasResource('X'))
    {
        //We know that we are boot-strapping for web.
        return;
    }

    //If we get here, we know that we are cli
    $this->bootstrap('TheDb');
    $this->bootstrap('Z');
}
于 2011-02-21T18:13:04.727 に答える
0

動作しているようです。cronに使用されるすべてのスクリプトに含める予定のinit.phpを作成しました。

<?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');

このファイルを含めた後、私は次のようなものを使用できるように見えます:

$db = Zend_Db_Table::getDefaultAdapter();

application.iniで設定する必要はないようです

resources.db.isDefaultTableAdapter = true

これは、ZendFrameworkのコードではデフォルトでtrueに設定されていると思います。

于 2011-02-22T16:09:13.837 に答える