symfony - Symfony 3.4 OneToMany SQLSTATE [23000]:完整性约束违规:1048 列“shopping_list_id”不能为空
问题描述
ShoppingList 包含许多 Fruits 并且 Fruits 包含在具有额外字段数量的许多 ShoppingList 中。
我创建了一个名为 ShoppingRow 的多对多实体,因此 ShoppingList 有 ManyToOne 和 ShoppingRow,ShoppingRow 有 ManyToOne 和 Fruits
在 ShoppingList 的操作 newAction 中,我遇到了一个问题:使用参数 [1,null,11] 执行“INSERT INTO shopping_row (quantity, shopping_list_id,fruit_id) VALUES (?, ?, ?)”时发生异常:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'shopping_list_id' cannot be null
源代码:
购物清单实体:
/**
* ShoppingList
*
* @ORM\Table(name="shopping_list")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ShoppingListRepository")
*/
class ShoppingList
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ShoppingList
* @ORM\OneToMany(targetEntity="ShoppingRow", mappedBy="shoppingList", cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $shoppingRows;
购物清单类型:
class ShoppingListType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('shoppingRows', CollectionType::class, array(
'entry_type' => ShoppingRowType::class,
'allow_add' => true,
))
;
}/**
ShoppingRow 实体:
/**
* ShoppingRow
*
* @ORM\Table(name="shopping_row")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ShoppingRowRepository")
*/
class ShoppingRow
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ShoppingList
*
* @ORM\ManyToOne(targetEntity="ShoppingList", inversedBy="shoppingRows")
* @ORM\JoinColumn(name="shopping_list_id", referencedColumnName="id", nullable=FALSE)
*/
private $shoppingList;
/**
* @var Fruit
*
* @ORM\ManyToOne(targetEntity="Fruit", inversedBy="shoppingRows")
* @ORM\JoinColumn(name="fruit_id", referencedColumnName="id", nullable=FALSE)
*/
private $fruit;
/**
* @var int
*
* @ORM\Column(name="quantity", type="integer")
*/
private $quantity;
购物行类型:
class ShoppingRowType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('quantity')
->add('fruit', EntityType::class, array(
'class' => 'AppBundle:Fruit',
'choice_label' => 'name',
));
;
}/**
购物清单控制器:
/**
* Creates a new shopping list entity.
*
* @Route("/new", name="shopping_list_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$shoppingList = new ShoppingList();
$form = $this->createForm('AppBundle\Form\ShoppingListType', $shoppingList);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($shoppingList);
$em->flush();
return $this->redirectToRoute('shopping_list_show', array('id' => $shoppingList->getId()));
}
return $this->render('shopping_list/new.html.twig', array(
'shoppingList' => $shoppingList,
'form' => $form->createView(),
));
}
解决方案
您必须手动设置相关实体。
$shoppingRow->setShoppingList($shoppingList)
因此,例如,如果您将 $shoppingRow 添加到现有的 $shoppingList,例如,从表单中,您需要获取该列表并将 shoppingList 添加到购物行。有很多方法可以确定它是哪个列表,这就是您的业务模型 - 无论您是否在列表中,您都可以通过参数转换器访问它,方法是在 URI 中传递它的 ID,或者通过表单的说 ChoiceType::您在数据库中查询现有列表的类。这是我们无能为力的地方,您需要自己解决这个问题。
现在,当您弄清楚如何到达您想要的列表时,您可以将代码放在控制器中,或者在某种处理您的 ShoppingLists 的服务中采取一种方法。
推荐阅读
- go - 创建 Go 结构字段“别名”的惯用方式是否只是自定义 getter?
- c# - 更改列表框中复选框的颜色
- python - JupyterLab 地理地图如何工作?
- c - 使用 OR 运算符时 for() 循环中断 (C)
- scripting - 上个季度的 Pine 脚本设置时间戳不起作用
- python - 接受 Youtube 的 cookie 同意
- python - 关键错误 22,当我想删除日期时间格式的行内容时出现获取 Loc 错误
- purescript - Purescript中遍历/绑定/折叠绑定效果的区别
- c - “警告:控制到达非无效函数的结尾”在一个简单的 for 循环(GCC)中
- php - Symfony 4.4 ContainerGWHmz1B\srcApp_KernelDevDebugContainer::getParameter(string $name) 声明必须兼容