php - Symfony 实体类型
问题描述
我尝试从我的 Form 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;
}
}
我的控制器:
/**
* @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
谢谢
解决方案
看起来您没有指定与供应商属性的关系。
在 Expense
实体属性供应商更改为此
/**
* @ManyToOne(targetEntity="Supplier")
* @JoinColumn(name="supplier_id", referencedColumnName="id")
*/
private $supplier;
不要忘记更新架构
如果这不起作用,请分享您的费用实体
推荐阅读
- excel - 不同计算机上图形的大小和位置 VBA EXCEL
- javascript - Vue 异步组件不适用于 Webpack 热模块替换
- django - 如何以 DRY 方式创建带有嵌套 url 的 rest api?
- python - 我正在使用简单的神经网络来解决 IRIS 数据集问题。NN 的输出是多标签多类而不是单标签
- reactjs - Sass @use - 多次导入同一个文件
- ios - 有什么方法可以在 macOS 中创建触控板事件
- visual-studio-code - VScode 在自动完成弹出窗口中显示可用项目的所有详细信息
- python - Geopandas:shapefile中现有图形中的颜色特定集群
- html - 调整大小时导航栏被切断
- powershell - 使用便携式 VSCode 和便携式 powershell