OxyScripts.com
Menu spacer Home Tutorials Articles Code Forums irc.freenode.net #oxyscripts
Main (PHP)
Home Forums PHP News PHP Tutorials Articles PHP Code Snippets Contact Us Sysadmin Resources Books Template Shop
3rd Party Streams
SlashDot PHPDeveloper.org PHP.Net
Resources
PHP Manual MySQL Manual Smarty Manual PEAR Manual PHP-GTK Manual Symfony Manual
Code Snippets
Authentication Database Graphics HTTP Miscellaneous Time/Date
Affiliates
Scripts TutorialMan TutorialGuide CodingForums.com PHP Scripts Cheap Web Hosting Affordable Web Hosting Dreamweaver Templates

Search This Site :     PHP Function Reference :
 

How to write a batch process

Overview

Creating batch processes in symfony is very simple. Batch processes can be used to automate any function that might need to be repeated on a regular basis. Some examples of useful batch functions might include a tool to load the development database with test data, or a production tool to periodically expire accounts, or send batched emails at a scheduled time.

Batch skeleton

In symfony, a batch process refers to any script that is run outside of the normal web front controller. To create a batch process, you need to define the location, application and environment that the batch process is to use.

This is easily accomplished by creating a PHP file in the myproject/batch/ directory and starting it with the following:

<?php
 
define('SF_ROOT_DIR',      realpath(dirname(__file__).'/..'));
define('SF_APP',           'myapp');
define('SF_ENVIRONMENT',   'prod');
define('SF_DEBUG',          false);
 
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
 
// initialize database manager
$databaseManager = new sfDatabaseManager();
$databaseManager->initialize();
 
// batch process here
?>

This script does nothing, or close to nothing: It defines a path, an application and an environment to get to a configuration, and loads that configuration. But that's already a lot because it means that all of the code written in your batch process will take advantage of the auto-loading of classes, automatic connection to Propel objects, and the symfony root classes.

Note: If you examine symfony's front controllers (like web/index.php) you will find this code extremely familiar. That's because every web request requires access to the same objects and configuration as a batch process.

The value that you define for SF_APP could be the name of any of the applications that you have defined in your project.

The SF_ENVIRONMENT has to be initialized with the name of the environment you want to run the batch in. The default possible environments are prod, dev, and test.

The SF_DEBUG constant defines whether the configuration has to be parsed every time the batch is run (true), or if a cached version can be used to imrpove speed (false). In general, the debug mode is used only in development. You can read more about the debug mode in the debug chapter.

Example batch processes

Below are some examples of batch processes that you might create.

Loading Test Data

Loading test data in the development and/or test environments is something that happens repeatedly. A PHP batch can automate this process. First, define the data to be loaded in a YAML file with the following format:

TableName:
  recordlabel:
    column:  value
    column:  value

Save one or more data files with this format in a myproject/data/fixtures/ directory. You can find more about how to create the data population files in the data files chapter.

The following script excerpt reads all YAML files in the fixtures/ directory and loads the data into the database defined in the app/config/databases.yml configuration file.

// batch process here
$data = new sfPropelData();
$data->loadData(sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'fixtures');

Processing

TODO

  • Create additional examples
  • Scheduling batches
 
   Print this page

Top Sponsor
Symantec\'s Norton SystemWorks 2006
Sponsors
CA
Sponsors
AdWords Dominator 125*125
Advertisting


Affiliates
VertexTemplates PHPFreaks CodeWalkers StarGeek DevScripts CGI & PHP Scripts PHP CMS Free Templates

Shopping Rebates   Sell It 4 You   Flash Page Counters   Get Insured
GPS Tracking Service   Charity Donate Info   Web Site Hosting   VOIP Service

Privacy Policy | Links | Site Map | Advertising

All content on OxyScripts.com is (©)2002-2007

 
Powered by Adrastea - Version 1.0.0. Copyright © Rune Solutions, 2004-2005