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.
<?php // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production')); require_once 'Zend/Application.php'; $application = new Zend_Application( APPLICATION_ENV, 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.
Thank you for sharing this code with us!