我有3个实体订单实体,产品实体和orderProducts实体,其中包含所有订购的产品。所以基本上orderProducts的映射是一个订单可以有很多产品。我想将orderProducts表单嵌入到订单中。 我面临的问题是没有显示嵌入的表单。
订单实体:
class Orders
{ ...
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="order_date", type="datetime")
*/
private $orderDate;
/**
* @var integer
*
* @ORM\ManyToOne(targetEntity="Recipient", inversedBy="orders", cascade={"persist"})
* @ORM\JoinColumn(name="recipient_id", referencedColumnName="id")
* @Assert\NotNull()
*/
private $recipientId;
...
/**
*
* @ORM\OneToMany(targetEntity="OrderProducts", mappedBy="orders")
*/
private $orderProducts;
/**
* Constructor
*/
public function __construct()
{
$this->orderProducts = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set orderDate
*
* @param \DateTime $orderDate
* @return Orders
*/
public function setOrderDate($orderDate)
{
$this->orderDate = $orderDate;
return $this;
}
/**
* Get orderDate
*
* @return \DateTime
*/
public function getOrderDate()
{
return $this->orderDate;
}
/**
* Set recipientId
*
* @param guid $recipientId
* @return Orders
*/
public function setRecipientId($recipientId)
{
$this->recipientId = $recipientId;
return $this;
}
/**
* Get recipientId
*
* @return guid
*/
public function getRecipientId()
{
return $this->recipientId;
}
....
/**
* Set orderProducts
*
* @param \Inventory\SystemBundle\Entity\OrderProducts $orderProducts
* @return Orders
*/
public function setOrderProducts(\Inventory\SystemBundle\Entity\OrderProducts $orderProducts = null)
{
$this->orderProducts = $orderProducts;
return $this;
}
/**
* Get orderProducts
*
* @return \Inventory\SystemBundle\Entity\OrderProducts
*/
public function getOrderProducts()
{
return $this->orderProducts;
}
/**
* Add orderProducts
*
* @param \Inventory\SystemBundle\Entity\OrderProducts $orderProducts
* @return Orders
*/
public function addOrderProduct(\Inventory\SystemBundle\Entity\OrderProducts $orderProducts)
{
$this->orderProducts[] = $orderProducts;
return $this;
}
/**
* Remove orderProducts
*
* @param \Inventory\SystemBundle\Entity\OrderProducts $orderProducts
*/
public function removeOrderProduct(\Inventory\SystemBundle\Entity\OrderProducts $orderProducts)
{
$this->orderProducts->removeElement($orderProducts);
}
}
产品实体
class Product
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
/**
*
* @ORM\OneToMany(targetEntity="OrderProducts", mappedBy="product")
*/
private $orderProducts;
...
public function __construct()
{
$this->orderProducts = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
...
/**
* Add orderProducts
*
* @param \Inventory\SystemBundle\Entity\OrderProducts $orderProducts
* @return Product
*/
public function addOrderProduct(\Inventory\SystemBundle\Entity\OrderProducts $orderProducts)
{
$this->orderProducts[] = $orderProducts;
return $this;
}
/**
* Remove orderProducts
*
* @param \Inventory\SystemBundle\Entity\OrderProducts $orderProducts
*/
public function removeOrderProduct(\Inventory\SystemBundle\Entity\OrderProducts $orderProducts)
{
$this->orderProducts->removeElement($orderProducts);
}
/**
* Get orderProducts
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getOrderProducts()
{
return $this->orderProducts;
}
}
orderProducts Entity
class OrderProducts
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Orders", inversedBy="order_products")
* @ORM\JoinColumn(name="order_id", referencedColumnName="id")
*/
private $orders;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="order_products")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
/**
* @var string
*
* @ORM\Column(name="quantity", type="string", length=255)
*/
private $quantity;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set order
*
* @return $this
*/
public function setOrders(Orders $orders)
{
$this->orders = $orders;
return $this;
}
/**
* Get order
*
* @return mixed
*/
public function getOrders()
{
return $this->orders;
}
/**
* Set product
*
* @return $this
*/
public function setProduct(Product $product)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return mixed
*/
public function getProduct()
{
return $this->product;
}
/**
* Set quantity
*
* @param string $quantity
* @return OrderProducts
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* @return string
*/
public function getQuantity()
{
return $this->quantity;
}
}
OrdersType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('orderDate', 'date', array('attr' => array('class' => 'form-control', 'style' => 'margin:10px;width:200px;height:25px;display:inline-block;')))
->add('recipientId', new RecipientType())
->add('orderProducts', 'collection', array(
'type' => new OrderProductsType(),
'options' => array('label' => false),
'cascade_validation' => true,
'allow_add' => true
))
//->add('orderProducts', CollectionType::class, array('entry_type' => OrderProductsType::class))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Inventory\SystemBundle\Entity\Orders'
));
}
/**
* @return string
*/
public function getName()
{
return 'orders';
}
OrderProductsType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('product', 'entity', array('label' => 'Product ','attr' => array('class' => 'form-control', 'style' => 'margin:10px;width:200px;height:25px;display:inline-block;'),
'class' => 'InventorySystemBundle:Product',
'choice_label' => 'product_name',
) )
->add('quantity', 'text', array('attr' => array('class' => 'form-control', 'style' => 'margin:10px;width:200px;height:25px;display:inline-block;')))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Inventory\SystemBundle\Entity\OrderProducts'
));
}
/**
* @return string
*/
public function getName()
{
return 'orderproducts';
}
来自OrdersController.php的createAction
public function createAction(Request $request)
{
$order = new Orders();
$orderProducts = new OrderProducts();
$order->addOrderProduct($orderProducts);
$request_all = $request->request->all();
$form = $this->createCreateForm($order);
try {
$form->handleRequest($request);
} catch (\Exception $e) {
echo "failed : ".$e->getMessage();
}
$recipientId = $request_all['inventory_systembundle_orders']['recipientId'];
$productId = $request_all['inventory_systembundle_orders']['product'];
$quantity = $request_all['inventory_systembundle_orders']['quantity'];
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($order);
$em->flush();
$product = $em->getRepository('InventorySystemBundle:Product')->find($productId);
$em = $this->getDoctrine()->getManager();
$orderProducts->setProduct($product);
$orderProducts->setOrders($order);
$orderProducts->setQuantity($quantity);
$em->persist($orderProducts);
$em->flush();
return $this->redirect($this->generateUrl('orders_new'));
}
return array(
'order' => $order,
'form' => $form->createView(),
);
}
我不确定我错过了什么。我不确定这些协会是否出错了。有人可以帮帮我吗?
twig file
{% extends '::base.html.twig' %}
{% block body -%}
<div align="right">
<a href="{{ path('orders') }}">
Back to the list
</a>
</div>
<h1>Orders creation</h1>
<div class="navbar-form navbar-left">
{{ form_start(form) }}
{{ form(form) }}
{{ form_end(form) }}
</div>
{% endblock %}
答案 0 :(得分:0)
我认为您需要添加一些JavaScript代码来呈现集合(您的 orderProducts 表单)
查看symfony文档,这很简单。 Symfony Doc - Embed a collection of forms