2016-09-16 76 views
0

試圖創建一個簡單的網絡商店,其中symfonydoctrine。問題是:我不知道如何創建應該有能力選擇/設置每種產品數量的訂單。我認爲我需要3個實體產品,訂單和OrderPosition。根據陣列集合中的實體創建表單

凡產品有價格和標題。訂單知道哪個用戶創建了一個訂單,並且在這種情況下相關的時間是訂單有訂單位置,訂單位置具有該特性數量。所以OrderPosition知道哪些產品已經訂購了多少次。

我不知道如果這些實體的映射/關係是正確的,所以我會在這裏告訴他們:

產品:

class Product 
{ 
    private $id; 
    private $price; 
    private $title; 
    private $orderPositions; 

    public function __construct() 
    { 
     $this->orderPositions = new ArrayCollection(); 
    } 

    public function getId() 
    { 
     return $this->id; 
    } 

    public function setPrice($price) 
    { 
     $this->price = $price; 

     return $this; 
    } 

    public function getPrice() 
    { 
     return $this->price; 
    } 

    public function setTitle($title) 
    { 
     $this->title = $title; 

     return $this; 
    } 

    public function getTitle() 
    { 
     return $this->title; 
    } 

    public function getOrderPositions() 
    { 
     return $this->orderPositions; 
    } 

    public function addOrderPosition(OrderPosition $orderPosition) 
    { 
     $this->orderPositions[] = $orderPosition; 
     if ($orderPosition->getProduct() !== $this) { 
      $orderPosition->setProduct($this); 
     } 
     return $this; 
    } 
} 

訂單:

class Order 
{ 
    private $id; 
    private $orderPositions; 

    public function __construct() 
    { 
     $this->orderPositions = new ArrayCollection(); 
    } 

    public function getId() 
    { 
     return $this->id; 
    } 

    public function getOrderPositions() 
    { 
     return $this->orderPositions; 
    } 

    public function addOrderPosition(OrderPosition $orderPosition) 
    { 
     $this->orderPositions[] = $orderPosition; 
     if ($orderPosition->getOrder() !== $this) { 
      $orderPosition->setOrder($this); 
     } 
     return $this; 
    } 
} 

OderPosition:

class OrderPosition 
{ 
    private $id; 
    private $quantity; 
    private $order; 
    private $product; 

    public function getId() 
    { 
     return $this->id; 
    } 

    public function getQuantity() 
    { 
     return $this->quantity; 
    } 

    public function setQuantity($quantity) 
    { 
     $this->quantity = $quantity; 
    } 

    public function getOrder() 
    { 
     return $this->order; 
    } 

    public function setOrder(Order $order) 
    { 
     $this->order = $order; 
     if ($order->getOrderPositions() !== $this) { 
      $order->addOrderPosition($this); 
     } 
     return $this; 
    } 

    public function getProduct() 
    { 
     return $this->product; 
    } 

    public function setProduct(Product $product) 
    { 
     $this->product = $product; 
     if ($product->getOrderPositions() !== $this) { 
      $product->addOrderPosition($this); 
     } 
     return $this; 
    } 
} 

映射文件:

產品:

MyBundle\Entity\Product: 
    type: entity 
    table: product 
    repositoryClass: MyBundle\Repository\ProductRepository 
    oneToMany: 
     orderPositions: 
      targetEntity: OrderPosition 
      mappedBy: product 
      cascade: [ "persist" ] 
    id: 
     id: 
      type: integer 
      id: true 
      generator: 
       strategy: AUTO 
    fields: 
     price: 
      type: float 
     title: 
      type: string 
      length: 255 
      column: title 

    lifecycleCallbacks: { } 

訂單:

MyBundle\Entity\Order: 
    repositoryClass: MyBundle\Repository\OrderRepository 
    type: entity 
    table: order 
    oneToMany: 
     orderPositions: 
      targetEntity: OrderPosition 
      mappedBy: order 
      cascade: [ "persist" ] 
    id: 
     id: 
      type: integer 
      id: true 
      generator: 
       strategy: AUTO 

    lifecycleCallbacks: { } 

OrderPosition:

MyBundle\Entity\OrderPosition: 
    repositoryClass: MyBundle\Repository\OrderPositionRepository 
    type: entity 
    table: order_position 
    manyToOne: 
     order: 
      targetEntity: Order 
      inversedBy: orderPositions 
      joinColumn: 
       name: order_id 
       referencedColumnName: id 
     product: 
      targetEntity: Product 
      inversedBy: orderPositions 
      joinColumn: 
       name: product_id 
       referencedColumnName: id 
    id: 
     id: 
      type: integer 
      id: true 
      generator: 
       strategy: AUTO 
    fields: 
     quantity: 
      type: integer 
    lifecycleCallbacks: { } 

,哪些應該創建表單控制器外觀像:

$order = new Order(); 

$form = $this->createFormBuilder($order) 
     ->add('quantity', OrderPositionType::class) 
     ->add('save', SubmitType::class, array('label' => 'Order')) 
     ->getForm(); 

和持續的OrderPositionType

class OrderPositionType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('quantity', IntegerType::class); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'MyBundle\Entity\OrderPosition' 
     )); 
    } 
} 

現在的問題是:我怎麼能拿到訂單的形式創建quantity輸入字段爲每個產品?

回答

2

你這樣定義類型:

OrderPositionType:

class OrderPositionType extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('quantity'); 
    } 
} 

訂單類型

class OrderType extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('orderPositions', CollectionType::class, [ 
      'entry_type' => OrderPositionType::class, 
      'allow_add' => false, 
     ]); 

     // Other fields then... 
    } 
} 

和控制器:

// $products = list of products user has selected. You already have it 
    $order = new Order(); 

    foreach ($products as $product) { 
     $orderPosition = new OrderPosition($order); 
     $orderPosition->setProduct($product); 
     $orderPosition->setQuantity(1); // default quantity, just my guess 
    } 

    $form = $this->createForm(OrderType::class, $order); 
+1

+1只是因爲你看過我的長問題。謝謝你,現在就試試吧。這是有道理的你正在寫什麼 – caramba

+0

你可能需要一個單獨的小部件OrderPositionType。因爲現在只能設置數量和刪除位置,而您可能只需要顯示有關產品的一些信息。如果您需要幫助,請告訴我 –

+0

謝謝德米特里!問題是最後一行'..buildForm..'導致buildForm是一個未定義的方法。如果我更改爲'..createFormBuilder..',我得到一個「可捕獲的致命錯誤:傳遞給createFormBuilder()的參數2必須是數組,但對象給出」..你能幫忙嗎? – caramba