Code snippets for symfony 1.x


Refine Tags

Snippets tagged "task"

Generate modules from all available doctrine models

As I didn't want to run doctrine:generate-module for all models over and over again I created a little task overwriting the default on. With this task you can create modules for all available models from project and plugins at once.

Add under PROJECT_DIR/lib/task the file sfDoctrineGenerateAllModulesTask.class.php with the following content:

class sfDoctrineGenerateAllModulesTask extends sfDoctrineGenerateModuleTask
   * @see sfTask
  protected function configure()
      new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
      new sfCommandOption('theme', null, sfCommandOption::PARAMETER_REQUIRED, 'The theme name', 'default'),
      new sfCommandOption('generate-in-cache', null, sfCommandOption::PARAMETER_NONE, 'Generate the module in cache'),
      new sfCommandOption('non-verbose-templates', null, sfCommandOption::PARAMETER_NONE, 'Generate non verbose templates'),
      new sfCommandOption('with-show', null, sfCommandOption::PARAMETER_NONE, 'Generate a show method'),
      new sfCommandOption('singular', null, sfCommandOption::PARAMETER_REQUIRED, 'The singular name', null),
      new sfCommandOption('plural', null, sfCommandOption::PARAMETER_REQUIRED, 'The plural name', null),
      new sfCommandOption('route-prefix', null, sfCommandOption::PARAMETER_REQUIRED, 'The route prefix', null),
      new sfCommandOption('with-doctrine-route', null, sfCommandOption::PARAMETER_NONE, 'Whether you will use a Doctrine route'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('actions-base-class', null, sfCommandOption::PARAMETER_REQUIRED, 'The base class for the actions', 'sfActions'),
    $this->namespace = 'doctrine';
    $this->name = 'generate-all-modules';
    $this->briefDescription = 'Generates all Doctrine modules';
    $this->detailedDescription = <<<EOF
The [doctrine:generate-all-modules|INFO] task generates all Doctrine modules:
  [./symfony doctrine:generate-all-modules frontend|INFO]
The task creates all modules in the [%application%|COMMENT] application
for all existing model classes [%model%|COMMENT].
You can also create empty modules that inherit their actions and templates from
a runtime generated module in [%sf_app_cache_dir%/modules/auto%module%|COMMENT] by
using the [--generate-in-cache|COMMENT] option:
  [./symfony doctrine:generate-all-modules --generate-in-cache frontend|INFO]
The generator can use a customized theme by using the [--theme|COMMENT] option:
  [./symfony doctrine:generate-all-modules --theme="custom" frontend|INFO]
This way, you can create your very own module generator with your own conventions.
You can also change the default actions base class (default to sfActions) of
the generated modules:
  [./symfony doctrine:generate-all-modules --actions-base-class="ProjectActions" frontend|INFO]
   * @see sfTask
  protected function execute($arguments = array(), $options = array())
    $databaseManager = new sfDatabaseManager($this->configuration);
    $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
    $models = $this->getModels();
    foreach ($models as $model) {
        foreach ($model as $name => $data) {
            $arguments['model'] = $name;
            $arguments['module'] = strtolower($name);
            $this->constants = array(
                'PROJECT_NAME'   => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony',
                'APP_NAME'       => $arguments['application'],
                'MODULE_NAME'    => $arguments['module'],
                'UC_MODULE_NAME' => ucfirst($arguments['module']),
                'MODEL_CLASS'    => $arguments['model'],
                'AUTHOR_NAME'    => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here',
            $method = $options['generate-in-cache'] ? 'executeInit' : 'executeGenerate';
            $this->$method($arguments, $options);
  protected function getModels() {
      $plugin_schemas = sfFinder::type('files')->name('schema.yml')->in(sfConfig::get('sf_plugins_dir'));
      $models = array();
      foreach($plugin_schemas as $schema) {
          $models[] = sfYaml::load($schema);
      $models[] = sfYaml::load(sfConfig::get('sf_config_dir') . '/doctrine/schema.yml');
      return $models;

Than just run: ./symfony doctrine:generate-all-modules %APPLICATION_NAME%

by Thomas Ohms on 2011-01-09, tagged doctrine  generate  module  task 

Render partials in a Task

I needed to send emails from a task using partials. The technique for SF 1.3/4 is explained in the docs, but how to get links to render properly is not. I found some good info on the web that was a bit hidden. Here is what it lead me to come up with.

To be able to use partials you need to set the context correctly:


Since tasks don't have a url as their base for the request, helpers like url_for and link_to won't work correctly and output things like "/symfony/symfony/mymodule/myaction".

to remedy this I created a helper:

class batchHelpers
  public static function url_for($str)
    $url = 'http://'.
            sfConfig::get('app_server_url','') .
    return $url;

the define for the "server_url" is in app.yml

Then in the partial I used this code to call get a correctly generated url:

batchHelpers::url_for('/home', true), array(

I also render the same partial (with the same url_for replacement) inside the web interface and since the urls are absolute, they work correctly.

by Alex Knol on 2010-10-26, tagged email  partial  task  urlfor 

Extending fix-perms

Sometimes we need more or different folders to be fix-permed. Mine is an easy hack, but having symfony freezed its not that ugly. The file is: /data/symfony/tasks/sfPakeMisc.php (line 41++)

function run_fix_perms($task, $args)
  $sf_root_dir = sfConfig::get('sf_root_dir');
  // just added this line for a folder
  pake_chmod("batch/any/folder", $sf_root_dir, 0777);
  pake_chmod(sfConfig::get('sf_cache_dir_name'), $sf_root_dir, 0777);
  pake_chmod(sfConfig::get('sf_log_dir_name'), $sf_root_dir, 0777);
  pake_chmod(sfConfig::get('sf_web_dir_name').DIRECTORY_SEPARATOR.sfConfig::get('sf_upload_dir_name'), $sf_root_dir, 0777);
  pake_chmod('symfony', $sf_root_dir, 0777);
  $dirs = array(
    // and this for chmoding files inside:  
    sfConfig::get('sf_root_dir') . "/batch/any/folder",  
  $dir_finder = pakeFinder::type('dir')->ignore_version_control();
  $file_finder = pakeFinder::type('file')->ignore_version_control();
  foreach ($dirs as $dir)
    pake_chmod($dir_finder, $dir, 0777);
    pake_chmod($file_finder, $dir, 0666);

Perhaps somebody uses a more elegant way ... ;)

Accessing just via:

> symfony fix-perms
by Axel Bernhardt on 2007-08-02, tagged fixperms  pake  task 



Mysql produce TINYINT fields because BOOLEAN is just a synonym for TINYINT. And if you have TINYINT fields with value 0 or 1 (like BOOLEAN fields) in your database and use command symfony propel-build-schema to generate schema, you will have one problem with admin generator, because it will produse TINYINT fields as text form fields with value - 0 or 1, not as checkboxes. Admin generator needs BOOLEAN fields in schema to produse checkboxes.


Solution to automate transformation field type tinyint to boolean where field name prefix is "is_".


pake_desc( 'apply tinyint-boolean transformation to your data model' );
pake_task( 'transform-schema-tinyint', 'project_exists' );
function run_transform_schema_tinyint( $task, $args ) 
    // Check params
    // -- missing params ?
    if ( !count($args) ) {
        throw new Exception( 'You must provide a transformation to apply.' );
    // -- schema exists ?
    $schema_filename = sprintf( '%s/schema.xml', sfConfig::get('sf_config_dir') );
    if ( !file_exists($schema_filename) ) {
        throw new Exception( "Missing schema.xml" );
    // Backup schema
    pake_copy( $schema_filename, $schema_filename . '.previous', array('override' => true) );
     //do hard work - tinyint->boolean
    if ($args[0] == 'do') {
        $handle = fopen($schema_filename, "r");
        $contents = '';
        while (!feof($handle)) {
          $contents .= fread($handle, 8192);
        $contents = preg_replace('/(name="is_.*?type=")TINYINT"/i','$1BOOLEAN"',$contents);
        $handle = fopen($schema_filename, "w+");
        fwrite($handle, $contents);
     //undo hard work - boolean->tinyint
    if ($args[0] == 'undo') {
        $handle = fopen($schema_filename, "r");
        $contents = '';
        while (!feof($handle)) {
          $contents .= fread($handle, 8192);
        $contents = preg_replace('/(name="is_.*?type=")BOOLEAN"/i','$1TINYINT"',$contents);
        $handle = fopen($schema_filename, "w+");
        fwrite($handle, $contents);

copy this code and drop it as new file in SF_DATA_DIR/tasks/ use command: symfony transform-schema-tinyint do to change tinyint to boolean, where field name with "is_" prefix, than rebuild your model with propel-build-model, clear cache, and use admin generator with checkboxes.

If something going wrong, do this command to undo changes in your model:

symfony transform-schema-tinyint undo (to change boolean to tinyint, where field name with "is_" prefix)

or you may use schema.xml.previous <- this is your schema before transformation

by Alex Gemini on 2006-08-28, tagged cli  generator  model  pake  task