【发布时间】:2022-11-22 21:32:37
【问题描述】:
我来这里是因为我找不到解决问题的办法。我在 Symfony 6 中有一个表单,其中一个值是 id_client 并引用到另一个实体 Client(关系 ManyToOne)。
我测试了几种方法来使该字段成为所有客户的选择(我显示客户的名称)。它们中的每一个都有效,但是当我提交表单时,这个值是作为整个实体添加的,而不仅仅是 id。这是一个问题,因为我以此结尾:
Expected argument of type "int", "App\\Entity\\Client" given at property path "id_client".
在我的表格中,它看起来像这样:
<?php
namespace App\Form;
use App\Entity\Client;
use App\Entity\Group;
use App\Repository\ClientRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class Group1Type extends AbstractType
{
private $clientRepository;
public function __construct(ClientRepository $clientRepository)
{
$this->clientRepository = $clientRepository;
$this->clients = $clientRepository->findAll();
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class, [
'attr' => [
'class' => 'form-control'
],
'label' => 'Name: '
])
->add('can_display', CheckboxType::class, [
'label' => 'Can display : ',
'attr' => [
'class' => 'my-3 mx-2'
]
])
->add('id_client', EntityType::class, [
'class' => Client::class,
// 'choices' => $this->clientRepository->findAllNameAlphabetical(),
// 'query_builder' => function (ClientRepository $client) {
// return $client->findAllNameAlphabetical();
// },
'choice_label' => 'name',
'expanded' => false,
'multiple' => false,
'attr' => [
'class' => 'form-control'
]
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Group::class,
]);
}
}
树枝:
<section class="container my-3">
<div class="row">
<div class="col">
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.can_display) }}
{{ form_row(form.id_client) }}
<button class="btn btn-primary my-3">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}
</div>
</div>
</section>
控制器(如果我让它与开始时一样,我会得到相同的结果):
#[Route('/new', name: 'app_group_new', methods: ['GET', 'POST'])]
public function new(Request $request, GroupRepository $groupRepository): Response
{
$group = new Group();
$form = $this->createForm(Group1Type::class, $group);
$form->handleRequest($request);
// $group->id_client = $group->id_client->id;
if ($form->isSubmitted()) {
// dd('submit');
// if(gettype($group->id_client)=="Client"){
// dd($group);
if($form->isValid()){
dd('valid');
$groupRepository->save($group, true);
$this->addFlash('success', 'The creation went successfully.');
return $this->redirectToRoute('app_group_index', [], Response::HTTP_SEE_OTHER);
// }
}
}
return $this->renderForm('group/new.html.twig', [
'group' => $group,
'form' => $form,
]);
}
我的实体:
#[ORM\Column]
private ?int $id_client = null;
【问题讨论】:
-
如果你只想要一个 id,那么你不应该使用
EntityType,因为它总是将id转换为一个实体,然后返回该实体。您可以改用ChoiceType,因为它不会进行自动转换。但是您确定您的实体设置正确吗?我希望Group有一个$client属性,该属性包含一个Client对象,然后 ORM 会将Client对象存储在数据库中时将其转换为id_client。 -
如果 $id_client 有关系,为什么要保存 int 而不是 Client 实体?你在混合概念,如果你想保存一个实体客户端,你应该有``` private ?Client $id_client = null; ``` 那么你可以在表单中使用 EntityType ,否则在表单中使用 ChoiceType 而不定义类属性。
-
你在这里错过了 symfony 和学说的多个概念。我真的推荐你阅读Symfony documentation。在 Symfony 中开发了 2 年后,我仍然每天都使用它并且我喜欢它。太棒了!
标签: php forms symfony entity symfony-forms