Code snippets for symfony 1.x


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