我尝试从表单EntityType获取。
将从实体获得名称和ID。
->add('supplier', EntityType::class, [
'class' => \App\Entity\Supplier::class,
'choice_value' => 'id',
'choice_label' => 'name',
])
我得到了预期的结果。
<select id="expense_supplier" name="expense[supplier]" class="form-control">
<option value="1">PayPal</option>
<option value="2">FB</option>
<option value="3">Yotube</option>
</select>
他正在获取Value AS整数。作为实体要求,
但是当我提交表格时。我遇到了错误:
给出类型为“整数”,“ App \ Entity \ Supplier”的预期参数。
我的供应商实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Supplier
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=50)
*/
private $name;
/**
* @ORM\Column(type="string", length=50)
*/
private $tax_id;
/**
* @ORM\Column(type="integer", length=25)
*/
private $category;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getTaxId(): ?string
{
return $this->tax_id;
}
public function setTaxId(string $tax_id): self
{
$this->tax_id = $tax_id;
return $this;
}
public function getCategory(): ?int
{
return $this->category;
}
public function setCategory(int $category): self
{
$this->category = $category;
return $this;
}
}
我的Contoller:
/**
* @Route("/expense/add", name="expense_add")
*/
public function add(Request $request): Response
{
$form = $this->createForm(\App\Form\ExpenseType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($form->getData());
$em->flush();
return $this->redirectToRoute('expense_list');
}
return $this->render('Admin/SuppliersTemplate.html.twig', [
'display' => 'form',
'title' => 'add.expense',
'form' => $form->createView()
]);
}
我尝试了很多方法,但是没有帮助
使用Symfony 4.1
谢谢