I’m working on a project that requires some batch data loads and export to excel functions. This was really the first time I’ve used Symfony batch.

Here are some random tips that helped me write my batch script. I think the Symfony book could use a bit more documentation detail when it comes to symfony batch as there are some things that aren’t even mentioned. Note: This is for Symfony 1.0.

Random Tip #1: Creating a symfony batch
I’m amazed how the documentation was lacking the details of what to put in the symfony batch, they just need a better explanation. So here is an example:

./symfony init-batch default symfony-nerds-look-good backend

In the example above, I’ve created a batch script called symfony-nerds-look-good, now, you might think that’s a problem in itself, but that’s another blog post! So the format of creating a batch job is of this is:

./symfony init-batch default scriptName appName

Note that ‘default’ is the name of the batch template. You can define multiple batch templates, but most of the time, the ‘default’ should be all that you need.

Random Tip #2: Symfony batch logs
If you are used to logging some custom logs with the Symfony logging classes in your view, its different for a batch component. In the actions class, this is all you have to do to write a log:

$this->logMessage("Creating spreadsheet...", "debug");

However, in batch you must do the following:

$logger = sfLogger::getInstance(); //firstly declare the instance of the sfLogger (otherwise you wont be able to log)
$logger->log("Creating spreadsheet", SF_LOG_DEBUG); //Then use the standard symfony log constance (eg SF_LOG_DEBUG or SF_LOG_WARN) etc...

So it is slightly different to how you might normally do it.

Random Tip #3: Using model objects and the database with batch
If you would like to use the generated ORM objects that Propel has generated for you, you wont be able to straight away. You will notice if you try to, you will get the error “No connection params set for propel”. You must first instantiate a sfDatabaseManager, then initialise it:

$databaseManager = new sfDatabaseManager();
$databaseManager->initialize();
//Then you can access your model objects and write your criteria as per the usual way

Hope this helps you get started. Do you have any tips with Symfony batch? Let us know!