【发布时间】:2017-09-24 17:17:16
【问题描述】:
使用 Symfony 3.2 和 FOS REST Bundle 我已经为资源创建了一些 REST 端点,并使用 Symfony 表单来定义 API DOC 的字段。到目前为止一切正常。 现在我正在尝试改进我的架构并向我的资源添加一个子实体(一对一)。我希望主资源保存子实体 - 子实体没有专用端点。
我按照the Symfony documentation 上的说明删除了所有其他字段以隔离任何问题。
这就是我的表单类型现在的样子:
<?php
namespace VendorName\MyBundle\Form;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CountryType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('mySubEntity', EntityType::class, array(
'multiple' => false,
'expanded' => false,
'property' => 'name',
'class' => 'MyBundle\Entity\mySubEntity'));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'VendorName\MyBundle\Entity\Country',
'csrf_protection' => false
));
}
}
现在,当我加载我的 api-docs 时,我收到了错误消息 The option "property" does not exist. Defined options are: "action", "allow_extra_fields", [...]。
老实说,我什至不知道将实体添加到表单是否是使其显示在 API 文档中的正确方法。任何帮助解决上述问题和/或实现这一目标的最佳实践将不胜感激。
编辑:感谢@miikes,此错误现已解决,我可以看到 api 文档正确显示嵌套表单的字段。但是,现在我的问题是表单没有填充父实体上的子实体。这似乎和我建模父子关系的方式有关,我发了new question for this issue。
【问题讨论】:
标签: php rest symfony fosrestbundle