0

我创建了自己的 GeneratorBundle(基于 SensioGeneratorBundle)来创建基于 Entity 的 FormType 类文件。它按照应有的方式生成文件,但问题是在 for 循环内部生成实体的字段,它将所有代码放在同一行。我希望在 for 循环中生成的代码位于每个字段的新行上。我尝试添加\n< br/>,但没有奏效。

命令 :

php app/console kuma:generate:adminlist Bundle:Entity

命令类:


    class GenerateAdminListCommand extends GenerateDoctrineCommand
    {   
         
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            
            $entity = $input->getOption('entity');
            list($bundle, $entity) = $this->parseShortcutNotation($entity);

            $entityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($bundle) . '\\' . $entity;
            $metadata = $this->getEntityMetadata($entityClass);
            
            $admintypeGenerator = $this->getAdminListTypeGenerator();
            $admintypeGenerator->generate($bundle, $entity, $metadata[0]);
            

我的发电机类:

    
    class AdminListTypeGenerator extends \Sensio\Bundle\GeneratorBundle\Generator\Generator
    {
        
        public function generate($bundle, $entity, $metadata)
        {
            
            $parameters = array('namespace' => $bundle->getNamespace(), 'bundle' => $bundle, 'entity_class' => $entityClass, 'fields' => $this->getFieldsFromMetadata($metadata));

            $this->renderFile($this->skeletonDir, 'EntityAdminListType.php', $dirPath . '/' . $entity . 'AdminListType.php', $parameters);
        }

扩展了以下类:


class Generator
{
    protected function render($skeletonDir, $template, $parameters)
    {
        $twig = new \Twig_Environment(new \Twig_Loader_Filesystem($skeletonDir), array(
            'debug'            => true,
            'cache'            => false,
            'strict_variables' => true,
            'autoescape'       => false,
        ));

        return $twig->render($template, $parameters);
    }

    protected function renderFile($skeletonDir, $template, $target, $parameters)
    {
        if (!is_dir(dirname($target))) {
            mkdir(dirname($target), 0777, true);
        }

        return file_put_contents($target, $this->render($skeletonDir, $template, $parameters));
    }
}

这些字段是从元数据中提取的:


    public static function getFieldsFromMetadata(ClassMetadata $metadata)
    {
        $fields = (array) $metadata->fieldNames;

        // Remove the primary key field if it's not managed manually
        if (!$metadata->isIdentifierNatural()) {
            $fields = array_diff($fields, $metadata->identifier);
        }

        foreach ($metadata->associationMappings as $fieldName => $relation) {
            if ($relation['type'] !== ClassMetadata::ONE_TO_MANY) {
                $fields[] = $fieldName;
            }
        }

        return $fields;
    }

在我的骨架文件中:


    
        public function buildForm(FormBuilder $builder, array $options) 
        {
            {%- for field in fields %}
                $builder->add('{{ field }}');
            {%- endfor %}
        }
    

这就是根据上面的骨架生成的 buildForm() 方法的外观:


    public function buildForm(FormBuilder $builder, array $options) 
    {   $builder->add('ip');    $builder->add('timestamp'); $builder->add('firstname'); $builder->add('lastname');  }

所以我想$builder->add('{{ field }}');在每次新迭代中创建一个新行。结果应如下所示:


    public function buildForm(FormBuilder $builder, array $options) 
    {           
        $builder->add('ip');
        $builder->add('timestamp');
        $builder->add('firstname');
        $builder->add('lastname');          
    }
4

2 回答 2

1

好的,似乎我在同事的帮助下找到了解决方案。

问题是树枝语法中的“-”:

{%- for field in fields %}

那个减号意味着它必须放在一条线上。删除减号就可以了。


    public function buildForm(FormBuilder $builder, array $options) 
    {
        {% for field in fields %}
        $builder->add('{{ field }}');
        {% endfor %}
    }

现在它呈现:


public function buildForm(FormBuilder $builder, array $options) 
    {
                $builder->add('ip');
                $builder->add('timestamp');
                $builder->add('firstname');
                $builder->add('lastname');
            }
于 2012-08-29T14:22:43.373 回答
0

你能试一下吗

 $builder->add('ip') .'<br/>'. $builder->add('timestamp'); 
于 2012-08-29T13:06:52.673 回答