我创建了自己的GeneratorBundle(基于SensioGeneratorBundle)来创建基于实体的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');
}
答案 0 :(得分: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');
}
答案 1 :(得分:0)
你能试试吗
$builder->add('ip') .'<br/>'. $builder->add('timestamp');