我尝试插入具有OneToMany关系的图像,但出现错误。
在属性路径“ images”中给出的类型为“ App \ Entity \ CategoryImage”,“ array”的预期参数。
我有实体Category和CategoryImage:
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CategoryImage", mappedBy="category", orphanRemoval=true ,cascade={"persist", "remove"})
*/
private $images;
.......
}
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryImageRepository")
*/
class CategoryImage
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $path;
/** @var \SplFileInfo */
protected $file;
....
}
两种形式:
class CategoryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$data = $builder->getForm()->getData();
$builder
->add('name')
->add('parent',OptionType::class,[
'class'=>Category::class,
'selectType'=>'flatexcept',
'required'=>false,
])
->add('images',CollectionType::class,[
'entry_type' => ImageTypeMultiple::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
]);
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Category::class,
]);
}
}
class ImageTypeMultiple extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options )
{
$builder->add('file',FileType::class,[
'label'=>false,
'required'=>false,
'constraints' => [
new Image([
'maxSize' => '5M'
])
]
]);
}
public function getBlockPrefix()
{
return 'image_multiple';
}
}
我的自定义ImageTypeMultiple form_theme小部件:
{% block image_multiple_widget -%}
{% spaceless %}
{% if form.vars.value.path|default(null) is null %}
<label for="{{ form.file.vars.id }}" class="image-upload btn btn-secondary btn-block"><i class="fa-cloud fa icon"></i> {{ 'select'|trans }}</label>
{% else %}
<img src="{{ form.vars.value.path|imagine_filter('sylius_small') }}" />
<label for="{{ form.file.vars.id }}" class="image-upload btn btn-secondary btn-block"><i class="fa-cloud fa icon"></i> {{ 'replace'|trans }}</label>
{% endif %}
<div class="d-none">
{{ form_widget(form.file) }}
</div>
<div class="element">
{{- form_errors(form.file) -}}
</div>
{% endspaceless %}
{% endblock %}
{% block collection_widget -%}
{% import _self as self %}
{% set attr = attr|merge({'class': attr.class|default ~ ' controls collection-widget'}) %}
{% spaceless %}
<div data-form-type="collection" {{ block('widget_container_attributes') }}
{% if prototype is defined and allow_add %}
data-prototype='{{ self.collection_item(prototype, allow_delete, 'Delete', '__name__')|e }}'
{%- endif -%}
>
{{ form_errors(form) }}
{% if prototypes|default is iterable %}
{% for key, subPrototype in prototypes %}
<input type="hidden" data-form-prototype="{{ key }}" value="{{ self.collection_item(subPrototype, allow_delete, button_delete_label, '__name__')|e }}" />
{% endfor %}
{% endif %}
<div data-form-collection="list" class="row">
{% for child in form %}
{{ self.collection_item(child, allow_delete, 'Delete', loop.index0) }}
{% endfor %}
</div>
{% if prototype is defined and allow_add %}
<a href="#" class="btn btn-primary" data-form-collection="add">
Add
</a>
{% endif %}
</div>
{% endspaceless %}
{%- endblock collection_widget %}
{% macro collection_item(form, allow_delete, button_delete_label, index) %}
{% spaceless %}
<div data-form-collection="item" data-form-collection-index="{{ index }}" class='col-lg-2 col-md-4 col-sm-6 imagetype_widget ml-3 mb-3' >
{{ form_widget(form) }}
{% if allow_delete %}
<a href="#" data-form-collection="delete" class="btn btn-warning btn-block">
<i class="fa fa-trash "></i>
Delete
</a>
{% endif %}
</div>
{% endspaceless %}
{% endmacro %}
我的控制器:
/**
* @Route("/{id}/edit", name="category_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Category $category): Response
{
$form = $this->createForm(CategoryType::class, $category);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
...}
}
我可以打开我的编辑页面而不会出现错误:
但是我在图像字段下选择图像并提交后,它显示此错误:
它显示
在属性路径“ images”处给出的类型为“ App \ Entity \ CategoryImage”,“ array”的预期参数。
在控制器中的$form->handleRequest($request)
行上,出现了问题,我已经解决这个问题好几天了,但是无法解决。