ECommerceCustomer.php 1.99 KB
Newer Older
piccoloprincipe's avatar
piccoloprincipe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php

namespace Doctrine\Tests\Models\ECommerce;

/**
 * ECommerceCustomer
 * Represents a registered user of a shopping application.
 *
 * @author Giorgio Sironi
 * @Entity
 * @Table(name="ecommerce_customers")
 */
class ECommerceCustomer
{
    /**
     * @Column(type="integer")
     * @Id
     * @GeneratedValue(strategy="AUTO")
     */
20
    private $id;
piccoloprincipe's avatar
piccoloprincipe committed
21 22 23 24

    /**
     * @Column(type="string", length=50)
     */
25
    private $name;
piccoloprincipe's avatar
piccoloprincipe committed
26 27

    /**
28
     * @OneToOne(targetEntity="ECommerceCart", mappedBy="customer", cascade={"persist"})
piccoloprincipe's avatar
piccoloprincipe committed
29
     */
30
    private $cart;
31 32 33 34 35

    /**
     * Example of a one-one self referential association. A mentor can follow
     * only one customer at the time, while a customer can choose only one
     * mentor. Not properly appropriate but it works.
36
     * 
37
     * @OneToOne(targetEntity="ECommerceCustomer", cascade={"persist"})
38 39 40
     * @JoinColumn(name="mentor_id", referencedColumnName="id")
     */
    private $mentor;
41 42 43 44 45 46 47 48 49 50 51 52
    
    public function getId() {
        return $this->id;
    }
    
    public function getName() {
        return $this->name;
    }
    
    public function setName($name) {
        $this->name = $name;
    }
piccoloprincipe's avatar
piccoloprincipe committed
53 54 55

    public function setCart(ECommerceCart $cart)
    {
56 57 58 59 60 61 62 63
        if ($this->cart !== $cart) {
            $this->cart = $cart;
            $cart->setCustomer($this);   
        }
    }
    
    /* Does not properly maintain the bidirectional association! */
    public function brokenSetCart(ECommerceCart $cart) {
piccoloprincipe's avatar
piccoloprincipe committed
64
        $this->cart = $cart;
65 66 67 68
    }
    
    public function getCart() {
        return $this->cart;
piccoloprincipe's avatar
piccoloprincipe committed
69 70 71 72
    }

    public function removeCart()
    {
73 74 75
        if ($this->cart !== null) {
            $cart = $this->cart;
            $this->cart = null;
romanb's avatar
romanb committed
76
            $cart->removeCustomer();
77
        }
piccoloprincipe's avatar
piccoloprincipe committed
78
    }
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    public function setMentor(ECommerceCustomer $mentor)
    {
        $this->mentor = $mentor;
    }

    public function removeMentor()
    {
        $this->mentor = null;
    }

    public function getMentor()
    {
        return $this->mentor;
    }
piccoloprincipe's avatar
piccoloprincipe committed
94
}