如何使用symfony admin generator在一个命令中生成所有模块?

时间:2011-05-02 12:19:35

标签: symfony-1.4

我正在使用symfony 1.4,doctrine orm。我想用单个symfony命令生成后端管理生成器。我有56张桌子。所以我想知道我必须执行56命令来创建后端模块?如何在单个命令中创建所有56个模块?

1 个答案:

答案 0 :(得分:0)

没有单一的命令。 您可以创建一个读取schema.yml的任务,从那里检索所有模型名称,并使用每个模型名称调用doctrine:generate-module任务。

以下是此类任务的示例:

class BuildAllModulesTask extends sfBaseTask
{
    protected function configure()
    {
        $this->addOptions(
            array(
                 new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'backend'),
                 new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
                 new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
            )
        );

        $this->namespace = 'ns';
        $this->name = 'build-all-modules';
        $this->aliases = array('bam');

        $this->briefDescription = 'Builds a module for each model in schema.yml';
        $this->detailedDescription = <<<EOF
The  Task [Builds a module for each model in schema.yml|INFO]
Call it with:

  [php symfony ns:build-all-modules|INFO]
EOF;

        $this->addOptions(
            array(
                 new sfCommandOption('app', null, sfCommandOption::PARAMETER_OPTIONAL, 'Application', 'backend')
            )
        );

    }

    /**
     *
     *
     * @param array $arguments
     * @param array $options
     * @return void
     */
    protected function execute($arguments = array(), $options = array())
    {
        $this->logSection('Step 1', 'Read all models from schema.yml');

        $yaml = new sfYamlParser();
        $models_array = $yaml->parse(file_get_contents(sfConfig::get('sf_config_dir') . '/doctrine/schema.yml'));
        $this->logSection('Step 1', 'There are ' . sizeof($models_array) . ' models in the schema.yml');

        $this->logSection('STEP 2', 'Go through all models from schema.yml and build an admin module in the "' . $options['app'] . '"');


        $sfDoctrineGenerateAdminTask = new sfDoctrineGenerateAdminTask($this->dispatcher, $this->formatter);

        $generate_options = array(
            'theme' => 'admin', // You can use here some other theme like jroller from ThemeJRoller plugin
            'env' => 'prod' // Here you can change to dev to see verbose output
        );

        foreach ($models_array as $model_name => $model_data) {
            if ($model_name == 'options') continue;

            $this->logSection('STEP 2', 'Processing "' . $model_name . '"');

            $args = array(
                'application' => $options['app'],
                'route_or_model' => $model_name,
            );
            $sfDoctrineGenerateAdminTask->run($args, $generate_options);

        }
    }
}