Symfony CollectionType 将删除+创建视为对某项的修改 [英] Symfony CollectionType regards deletion+creation as a modification of an item
问题描述
我有一个 Symfony OrderType
表单附加到 Order
对象.这个 Order
对象有一个 items
属性,它包含一个 OrderItem
对象的 ArrayCollection
.这些 OrderItem
对象都包含 product
和 quantity
属性.product
属性链接到 Product
对象.每个 OrderItem
都与一个 OrderItemType
相关联,OrderItemType
是与 Order
items<相关联的
CollectionType
的一部分/code> 属性.
I have a Symfony OrderType
form attached to an Order
object. This Order
object has an items
property which contains an ArrayCollection
of OrderItem
objects. These OrderItem
objects each contain product
and quantity
properties. The product
property is linked to a Product
object. Each OrderItem
is associated to an OrderItemType
which is part of the CollectionType
associated to the Order
items
property.
长话短说,这是代码(非常简化,具有最少的属性并且没有学说注释):
To make a long story short, here is the code (very simplified, with minimal properties and no doctrine annotations):
请注意,一切基本正常,我不是在寻找这段代码中的错误:)
class Order {
private $id;
private $items;// ArrayCollection of OrderItem
}
订单项目类别
class OrderItem {
private $id;
private $product;// Product object
private $quantity;// int
}
订购单
class OrderType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('items', CollectionType::class, array(
'by_reference' => false,
'entry_type' => OrderItemType::class,
'allow_add' => true,
'allow_delete' => true
));
}
}
订单项表单
class OrderItemType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('product', EntityType::class, array(
'class' => 'AppBundle\Entity\Product',
'choice_label' => 'id'
))
->add('quantity', NumberType::class)
}
}
当然,用户可以按照适当的创建、编辑和删除行的顺序添加和删除行.好吧几乎...
Of course, the user can add and delete lines in an order which creates, edits and removes lines appropriately. Well almost...
如果我删除订单的最后一行并创建一个新行,Symfony 认为新行实际上是对旧行的修改.当然,Symfony 没有办法知道这一行不是旧的.
If I remove the last line of the order and create a new line, Symfony thinks that the new line is actually a modification of the old one. Of course, there is no way for Symfony to know that this line is not the old one.
所以我在我的 OrderItemType
中添加了一个 id
字段.但是当然,Symfony 不能在提交时设置 id
因为在 OrderItem
中没有 setId
方法(这是正常的,因为设置 id
一个对象手动是一个坏主意).
So I added an id
field to my OrderItemType
. But of course, Symfony cannot set the id
on submit because there is no setId
method in OrderItem
(and that's normal because setting the id
of an object manually is a bad idea).
好吧,这就是我被卡住的地方.我想我可以将 id
字段设置为 mapped: false
并在控制器上实现自定义逻辑以摆脱在 id<时被删除的旧项目/code> 没有出现在发布的值中,并为具有空
id
的项目创建一个新的 OrderItem
,但这对我来说似乎是丑陋的黑客攻击,我想知道我并没有完全遗漏一些东西,因为这看起来是一个非常基本的问题.
Well that's where I'm stuck. I guess I could set the id
field as mapped: false
and implement custom logic on the controller to get rid of the old item that was deleted when its id
does not appear in the posted values and create a new OrderItem
for the item that has an empty id
, but this seems like ugly hacking to me and I wonder if I'm not completely missing something as this looks like a very basic problem.
那么Symfony 是否有内置的方法来识别CollectionType
表单中删除+创建 和版本 之间的区别?我完全错过了什么吗?蛋糕是骗人的吗?
So does Symfony have a built-in way to recognize the difference between deletion+creation and edition in a CollectionType
form? Am I completely missing something? Is the cake a lie?
我知道我的代码不完整,但我想专注于手头的问题.换一种说法:
I know that my code is not complete, but I want to focus on the problem at hand. To put in another way:
- 用户为需要 3 个产品 A 和 2 个产品 B 的客户在订单中创建了两行.
- 用户保存订单(Symfony 在数据库中创建了 2 个
OrderItem
对象,ID 为 1 和 2) - 客户再次致电并想要 5 个 productC 而不是 2 个 productB.
- 用户删除了第 2 行(2 个 productB)并添加了一个新行(5 个 productC).
- The user creates two lines in the order for a client that wants 3 productA and 2 productB.
- The user saves the order (Symfony creates 2
OrderItem
objects with ID 1 and 2 in the database) - The client calls again and wants 5 productC instead of the 2 productB.
- The user deletes line 2 (2 productB) and adds a new line (5 productC).
这里,Symfony 通过分配新产品 (productC) 和数量 (5) 来更改 ID #2 的 OrderItem
.但我希望 Symfony 删除 OrderItem
#2 并创建一个 ID 为 3 的新 OrderItem
.
Here, Symfony changes the OrderItem
with ID #2 by assigning the new product (productC) and quantity (5). But I would like Symfony to delete OrderItem
#2 and create a new OrderItem
with ID 3.
我希望这更清楚一点.
推荐答案
我知道您还没有添加 Doctrine 注释,但这与您描述的 Id 问题有关.通常你会自动生成一个 ID,所以没有setter",但有一个getter".
I know you haven't added your Doctrine annotations yet, but it's related to the Id problem you are describing. Normally you auto generate an ID, so there is no 'setter', but there is a 'getter'.
所以你会有这样的东西:
So you would have something like this:
class OrderItem {
...
/**
* @ORM\Id
* @ORM\Column(name="order_item_id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $order_item_id;
是的,您的 $quantity 值应该是一个int",但这并不重要.我只是让你知道你不能有一半的数量.
Yes, your $quantity value should be an 'int', but it doesn't really matter. I'm just making you aware that you can't have half a quantity.
这篇关于Symfony CollectionType 将删除+创建视为对某项的修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!