A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Zend Framework: Using Command Line Scripts

Thanks to David Caunt for this very useful article.

The article shows us how to create command line scripts using Zend Framework components. This is very useful especially with the database stuff.

// Define path to application directory
    || define('APPLICATION_PATH',
              realpath(dirname(__FILE__) . '/../application'));
// Define application environment
    || define('APPLICATION_ENV',
              (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
                                         : 'production'));
require_once 'Zend/Application.php';
$application = new Zend_Application(
    APPLICATION_PATH . '/configs/config.php'
//only load resources we need for script, in this case db and mail
$application->bootstrap(array('db', 'mail'));

Using it would be something like this:

$db = $application->getBootstrap()->getResource('db');
//do something!
$row = $db->fetchRow('SELECT * FROM something');

The source can be found here. Sorry for the copy-paste. This is so useful that I want to see the code every time I forget.

Thanks to Rob Allen for directing me to David Caunt.

This entry was posted in General and tagged , , , . Bookmark the permalink.

One Response to Zend Framework: Using Command Line Scripts

  1. Thank you for sharing this code with us!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>