2017-12-19 51 views
1

私はdoctrineでカートを作成しようとしています。今、私は "数量"で立ち往生しています。 商品がカートに入っていれば、数量(数量+ 1)を更新しようとしています。ここでSymfony/Doctrine - エンティティ内の関連エンティティから行を取得

は私のエンティティは以下のとおりです。

Cart.php

class Cart 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="UUID") 
    * @ORM\Column(type="guid") 
    */ 
    private $id; 

    /** 
    * @ORM\OneToOne(targetEntity="Order", inversedBy="cart", cascade={"persist"}) 
    * @ORM\JoinColumn() 
    */ 
    private $order; 

    /** 
    * @ORM\OneToMany(targetEntity="CartItem", mappedBy="cart", cascade={"persist"}) 
    */ 
    private $cartItems; 

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

    ... 

    public function getItems() 
    { 
     return $this->cartItems; 
    } 

    public function addItem(CartItem $cartItem, Product $product, int $quantity = 1) 
    { 
     if ($this->cartItems->contains($cartItem)) 
      return; 

     $cartItem->setProduct($product); 
     $cartItem->setQuantity($quantity); 
     $cartItem->setBoughtPrice($product->getBoughtPrice()); 
     $cartItem->setPrice($product->getPrice()); 

     $this->cartItems[] = $cartItem; 
     // set the *owning* side! 
     $cartItem->setCart($this); 
    } 

    public function removeItem(CartItem $cartItem) 
    { 
     $this->cartItems->removeElement($cartItem); 
     // set the owning side to null 
     $cartItem->setCart(null); 
    } 
} 

CartItem.php

class CartItem 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="UUID") 
    * @ORM\Column(type="guid") 
    */ 
    private $id; 

    ... 

    /** 
    * @ORM\ManyToOne(targetEntity="Cart", inversedBy="cartItems") 
    * @ORM\JoinColumn(name="cart_id", referencedColumnName="id") 
    */ 
    private $cart; 

    /** 
    * @ORM\ManyToOne(targetEntity="App\Entity\Product\Product", inversedBy="cartItems") 
    * @ORM\JoinColumn(name="product_id", referencedColumnName="id") 
    */ 
    private $product; 

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

    ... 

    public function getCart() 
    { 
     return $this->cart; 
    } 

    public function setCart(Cart $cart) 
    { 
     $this->cart = $cart; 
    } 

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

    public function setProduct(Product $product) 
    { 
     $this->product = $product; 
    } 

    ... 

} 

私が最も重要な方法は、のaddItem()Cart.phpでだと思います。

関連するエンティティからすべての行にアクセスし、製品がすでに存在するかどうかを比較できますか?

コントローラで行う必要がありますか?

答えて

1

次のコードを試してみてください。このヘルプ

public function addItem(CartItem $cartItem, Product $product, int $quantity = 1) 
{ 
    if ($this->cartItems->contains($cartItem)) 
     return; 

    // Looking for an item with the same product 
    foreach ($this->cartItems as $item) { 
     // Suppose the product are equals comparing it by id 
     if ($item->getProduct()->getId() === $product->getId()) { 
      // We find an existing cart item for the product 
      // Update the cart item info: 
      $cartItem->setQuantity($cartItem->getQuantity() + $quantity); 
      // NB: should we take care of the quantity ? 
      $cartItem->setBoughtPrice($cartItem->getBoughtPrice() + $product->getBoughtPrice()); 
      // NB: should we take care of the quantity ? 
      $cartItem->setPrice($cartItem->getPrice() + $product->getPrice()); 

      return; 
     } 
    } 

    $cartItem->setProduct($product); 
    $cartItem->setQuantity($quantity); 
    $cartItem->setBoughtPrice($product->getBoughtPrice()); 
    $cartItem->setPrice($product->getPrice()); 

    $this->cartItems[] = $cartItem; 
    // set the *owning* side! 
    $cartItem->setCart($this); 
} 

・ホープ