2017-12-19 33 views
1

doctrine에서 장바구니를 만들려고합니다. 이제 나는 "양"에 붙어 있습니다. 제품이 이미 장바구니에 있으면 수량 (수량 + 1)을 업데이트하려고합니다. 나는 가장 중요한 방법은 Cart.php에서 addItem를()생각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; 
    } 

    ... 

} 

: 여기

내 엔티티입니다.

관련 엔터티의 모든 행에 액세스하여 제품이 이미 존재하는지 비교할 수 있습니까?

아니면 컨트롤러에서해야합니까? 다음 코드로

답변

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); 
} 

희망이 도움