phpzend-frameworkcommand-line-interfacezend-dbzend-application

script create Zend_Db_Adapter based on settings in application.ini for cli php


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.


Solution

  • 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.