ECommerceCart.php 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Doctrine\Tests\Models\ECommerce;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. /**
  5. * ECommerceCart
  6. * Represents a typical cart of a shopping application.
  7. *
  8. * @author Giorgio Sironi
  9. * @Entity
  10. * @Table(name="ecommerce_carts")
  11. */
  12. class ECommerceCart
  13. {
  14. /**
  15. * @Column(type="integer")
  16. * @Id
  17. * @GeneratedValue
  18. */
  19. private $id;
  20. /**
  21. * @Column(length=50, nullable=true)
  22. */
  23. private $payment;
  24. /**
  25. * @OneToOne(targetEntity="ECommerceCustomer", inversedBy="cart")
  26. * @JoinColumn(name="customer_id", referencedColumnName="id")
  27. */
  28. private $customer;
  29. /**
  30. * @ManyToMany(targetEntity="ECommerceProduct", cascade={"persist"})
  31. * @JoinTable(name="ecommerce_carts_products",
  32. joinColumns={@JoinColumn(name="cart_id", referencedColumnName="id")},
  33. inverseJoinColumns={@JoinColumn(name="product_id", referencedColumnName="id")})
  34. */
  35. private $products;
  36. public function __construct()
  37. {
  38. $this->products = new ArrayCollection;
  39. }
  40. public function getId() {
  41. return $this->id;
  42. }
  43. public function getPayment() {
  44. return $this->payment;
  45. }
  46. public function setPayment($payment) {
  47. $this->payment = $payment;
  48. }
  49. public function setCustomer(ECommerceCustomer $customer) {
  50. if ($this->customer !== $customer) {
  51. $this->customer = $customer;
  52. $customer->setCart($this);
  53. }
  54. }
  55. public function removeCustomer() {
  56. if ($this->customer !== null) {
  57. $customer = $this->customer;
  58. $this->customer = null;
  59. $customer->removeCart();
  60. }
  61. }
  62. public function getCustomer() {
  63. return $this->customer;
  64. }
  65. public function getProducts()
  66. {
  67. return $this->products;
  68. }
  69. public function addProduct(ECommerceProduct $product) {
  70. $this->products[] = $product;
  71. }
  72. public function removeProduct(ECommerceProduct $product) {
  73. return $this->products->removeElement($product);
  74. }
  75. }