【问题标题】:Symfony / Doctrine - Get rows from related entity in entitySymfony / Doctrine - 从实体中的相关实体获取行
【发布时间】:2017-12-19 07:04:51
【问题描述】:

我正在尝试在教义中创建购物车。现在我被“数量”困住了。 如果产品已经在购物车中,我正在尝试实现这一点,请更新数量(数量 + 1)。

这是我的实体:

购物车.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;
    }

    ...

}

我认为最重要的方法是 Cart.php 中的 addItem()

是否可以访问相关实体的所有行并比较产品是否已经存在?

或者我应该在控制器中执行此操作?

【问题讨论】:

    标签: php symfony doctrine-orm doctrine


    【解决方案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);
    }
    

    希望有帮助

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多