Symfony2 更新“子窗体”中的项目一些内部要检查的内容
我的问题的简短版本:
如何在 Symfony2 中编辑子窗体的实体?
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
我有一个实体订单
<?php
class Order
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Customer")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id", nullable=false)
**/
private $customer;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="date")
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity="\AppBundle\Entity\OrderStatus")
* @ORM\JoinColumn(name="order_status_id", referencedColumnName="id", nullable=false)
**/
private $orderStatus;
/**
* @var string
*
* @ORM\Column(name="reference", type="string", length=64)
*/
private $reference;
/**
* @var string
*
* @ORM\Column(name="comments", type="text")
*/
private $comments;
/**
* @var array
*
* @ORM\OneToMany(targetEntity="OrderRow", mappedBy="Order", cascade={"persist"})
*/
private $orderRows;
...
}
我的SQL
_____________________________________________________________ |id | order id | |customer_id | fk customer.id NOT NULL | |date | order date | |order_status_id | fk order_status.id NOT NULL | |reference | varchar order reference | |comments | text comments | |___________________________________________________________|
和实体 OrderRow(一个订单可以有一行或多行)
<?php
class OrderRow
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Order", inversedBy="orderRows", cascade={"persist"})
* @ORM\JoinColumn(name="order_id, referencedColumnName="id", nullable=false)
**/
private $order;
/**
* @ORM\ManyToOne(targetEntity="[MyShop\Bundle\ProductBundle\Entity\Product")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true)
**/
private $product;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255)
*/
private $description;
/**
* @var integer
*
* @ORM\Column(name="count", type="integer")
*/
private $count = 1;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="date")
*/
private $date;
/**
* @var decimal
*
* @ORM\Column(name="amount", type="decimal", precision=5, scale=2)
*/
private $amount;
/**
* @var string
*
* @ORM\Column(name="tax_amount", type="decimal", precision=5, scale=2)
*/
private $taxAmount;
/**
* @var string
*
* @ORM\Column(name="discount_amount", type="decimal", precision=5, scale=2)
*/
private $discountAmount;
...
}
我的SQL
_____________________________________________________________ |id | order id | |order_id | fk order.id NOT NULL | |product_id | fk product.id | |description | varchar product description | |count | int count | |date | date | |amount | amount | |taxAmount | tax amount | |discountAmount | discount amount | |___________________________________________________________|
我想创建一个表单,允许编辑一个订单及其行。
订单类型.php
class OrderType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('customer', 'entity', array(
'class' => 'Customer',
'multiple' => false
))
->add('orderStatus', 'entity', array(
'class' => 'AppBundle\Entity\OrderStatus',
'multiple' => false
))
->add('date')
->add('reference')
->add('comments')
->add('orderRows', 'collection', [
'type' => new OrderRowType(),
'allow_add' => true,
'by_reference' => false,
])
;
}
...
}
OrderRowType.php
class OrderRowType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('order', 'entity', array(
'class' => 'MyShop\Bundle\OrderBundle\Entity\Order',
'multiple' => false
))
->add('product', 'product_selector') // service
->add('orderRowStatus', 'entity', array(
'class' => 'AppBundle\Entity\OrderRowStatus',
'multiple' => false
))
->add('description')
->add('count')
->add('startDate')
->add('endDate')
->add('amount')
->add('taxAmount')
->add('discountAmount')
;
}
...
}
更新订单是通过向我的 API 发送请求来完成的:
- 请求网址: https://api.example.net/admin/orders/update/37
- 请求方法: 开机自检
-
状态代码:200
Params: { "order[customer]": "3", "order[orderStatus]": "1", "order[date][month]:": "5", "order[date][day]": "18", "order[date][year]": "2015", "order[reference]": "Testing", "order[comments]": "I have nothing to say!", "order[orderRows][0][order]": "32", "order[orderRows][0][product]": "16721", "order[orderRows][0][orderRowStatus]:1": "1", "order[orderRows][0][description]": "8 GB memory", "order[orderRows][0][count]": "12", "order[orderRows][0][startDate][month]": "5", "order[orderRows][0][startDate][day]": "18", "order[orderRows][0][startDate][year]": "2015", "order[orderRows][0][endDate][month]": "5", "order[orderRows][0][endDate][day]": "18", "order[orderRows][0][endDate][year]": "2015", "order[orderRows][0][amount]": "122.03", "order[orderRows][0][taxAmount]": "25.63", "order[orderRows][0][discountAmount]": "0", "order[orderRows][1][order]": "32", "order[orderRows][1][product]": "10352", "order[orderRows][1][orderRowStatus]": "2", "order[orderRows][1][description]": "12 GB MEMORY", "order[orderRows][1][count]": "1", "order[orderRows][1][startDate][month]": "5", "order[orderRows][1][startDate][day]": "18", "order[orderRows][1][startDate][year]": "2015", "order[orderRows][1][endDate][month]": "5", "order[orderRows][1][endDate][day]": "18", "order[orderRows][1][endDate][year]": "2015", "order[orderRows][1][amount]": "30.8", "order[orderRows][1][taxAmount]": "6.47", "order[orderRows][1][discountAmount]": "0", "order[orderRows][2][order]": "32", "order[orderRows][2][product]": "2128", "order[orderRows][2][orderRowStatus]": "3", "order[orderRows][2][description]": "4GB MEMORY", "order[orderRows][2][count]": "5", "order[orderRows][2][startDate][month]": "5", "order[orderRows][2][startDate][day]": "18", "order[orderRows][2][startDate][year]": "2015", "order[orderRows][2][endDate][month]": "5", "order[orderRows][2][endDate][day]": "18", "order[orderRows][2][endDate][year]": "2015", "order[orderRows][2][amount]": "35.5", "order[orderRows][2][taxAmount]": "7.46", "order[orderRows][2][discountAmount]": "0" }
上面的请求会编辑订单详细信息并创建新的order_rows,因为未提供任何order_row_id。在Symfony2中的Nowere中,我发现我应该$builder->add('id')到我的OrderRowType,我的实体也没有列ID的设置器。
在了解了大量信息之后,我有一个非常简短的问题。我应该如何更新此表单中的order_rows记录?