简体   繁体   English

带有原理的购物车功能

[英]Cart feature with Doctrine

I'm trying to implement a Cart feature in my Symfony app. 我正在尝试在Symfony应用中实现购物车功能。 The purpose is to allow a User to add some events in a Cart. 目的是允许用户在购物车中添加一些事件。

So I have created 3 Entities. 因此,我创建了3个实体。 User , Event and Cart . UserEventCart A User need to access his Cart to get his events. 用户需要访问他的购物车来获取他的事件。 Like $user->getCart , which will return an ArrayCollection of events. 就像$user->getCart ,它将返回事件的ArrayCollection。

I have no idea what is the best way to do it with the Doctrine relation. 我不知道用教义关系做这件事的最好方法是什么。 Everything I have tried does not seems to work. 我尝试过的所有方法似乎都无效。

Here is what I have made so far: 到目前为止,这是我所做的:

In my User Entity 在我的User实体中

/**
* @ORM\OneToOne(targetEntity="App\Entity\Cart", mappedBy="user", cascade={"persist", "remove"})
*/
private $cart;

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

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

    // set the owning side of the relation if necessary
    if ($this !== $cart->getUser()) {
        $cart->setUser($this);
    }

    return $this;
}

In my User Entity 在我的User实体中

/**
* @ORM\OneToOne(targetEntity="App\Entity\User", inversedBy="cart", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $user;

I have stopped here, because I feel like I'm not doing the right approach. 我已经在这里停了下来,因为我觉得我没有采取正确的方法。

May I have your feeling about it? 我对此有何感想?

Although I haven't tested it myself, it will highly likely work. 尽管我自己还没有测试过,但是很有可能会工作。

Entities: 实体:

class User
{
    ...

    /**
     * @ORM\OneToMany(targetEntity="Cart", mappedBy="user", cascade={"persist"})
     */
    private $carts;

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

    public function addCart(Cart $cart): self
    {
        $this->carts[] = $cart;

        return $this;
    }

    public function removeCart(Cart $cart): bool
    {
        return $this->carts->removeElement($cart);
    }

    public function getCarts(): Collection
    {
        return $this->carts;
    }
}

class Cart
{
    ...

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="carts", cascade={"persist"})
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
     */
    private $user;

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

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

    public function setUser(User $user): self
    {
        $this->user = $user;

        return $this;
    }

    public function getUser(): User
    {
        return $this->user;
    }

    public function addEvent(Event $event): self
    {
        $this->events[] = $event;

        return $this;
    }

    public function removeEvent(Event $event): bool
    {
        return $this->events->removeElement($event);
    }

    public function getEvents(): Collection
    {
        return $this->events;
    }
}

class Event
{
    ...

    /**
     * @ORM\ManyToOne(targetEntity="Cart", inversedBy="events", cascade={"persist"})
     * @ORM\JoinColumn(name="cart_id", referencedColumnName="id", nullable=false)
     */
    private $cart;

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

        return $this;
    }

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

When doing carts and e-commerces you have to keep in mind a lot of things, and need to ask yourself what kind of information you want to persist. 在进行手推车和电子商务时,您必须记住很多事情,并且需要问自己要保留什么样的信息。 Some people develop Cart modules in a Event Sourced way so they don't loose any data. 有些人以事件源方式开发购物车模块,因此他们不会丢失任何数据。 This talk by Greg Young is great on the subject. 格雷格·杨(Greg Young)的演讲在这个主题上很棒。 That's the approach I would use, but is not the easiest one. 那是我要使用的方法,但不是最简单的方法。

But maybe you don't want to persist all that valuable extra state. 但是也许您不想保留所有这些有价值的额外状态。 In that case, you can use the traditional CRUD approach as you are trying to. 在这种情况下,您可以尝试使用传统的CRUD方法。

When using this, keep in mind two things. 使用此功能时,请记住两件事。

  1. Your events are mutable. 您的事件是可变的。 Price can change, location can change, time can change. 价格可以改变,位置可以改变,时间可以改变。
  2. When the purchase is complete, you will take that cart and generate a Purchase object from it. 购买完成后,您将使用该购物车并从中生成一个Purchase对象。 That Purchase object CAN NOT have any relationship to Event , because of 1. 由于1,该Purchase对象不能与Event有任何关系。
  3. You don't want a user to have a single Cart, I guess. 我想您不希望用户拥有一个购物车。 A User can have many Carts, so maybe you should make that relationship a ManyToOne on the cart side. 一个用户可以有多个购物车,因此也许您应该在购物车一侧将这种关系设置为ManyToOne Otherwise, Cart can be just a value object that you can store in user. 否则,购物车只能是您可以存储在用户中的值对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM