1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | |
---|
21 | namespace Doctrine\ORM\Mapping\Builder; |
---|
22 | |
---|
23 | use Doctrine\ORM\Mapping\ClassMetadata; |
---|
24 | |
---|
25 | class AssociationBuilder |
---|
26 | { |
---|
27 | /** |
---|
28 | * @var ClassMetadataBuilder |
---|
29 | */ |
---|
30 | protected $builder; |
---|
31 | |
---|
32 | /** |
---|
33 | * @var array |
---|
34 | */ |
---|
35 | protected $mapping; |
---|
36 | |
---|
37 | /** |
---|
38 | * @var array |
---|
39 | */ |
---|
40 | protected $joinColumns; |
---|
41 | |
---|
42 | /** |
---|
43 | * |
---|
44 | * @var int |
---|
45 | */ |
---|
46 | protected $type; |
---|
47 | |
---|
48 | /** |
---|
49 | * @param ClassMetadataBuilder $builder |
---|
50 | * @param array $mapping |
---|
51 | */ |
---|
52 | public function __construct(ClassMetadataBuilder $builder, array $mapping, $type) |
---|
53 | { |
---|
54 | $this->builder = $builder; |
---|
55 | $this->mapping = $mapping; |
---|
56 | $this->type = $type; |
---|
57 | } |
---|
58 | |
---|
59 | public function mappedBy($fieldName) |
---|
60 | { |
---|
61 | $this->mapping['mappedBy'] = $fieldName; |
---|
62 | return $this; |
---|
63 | } |
---|
64 | |
---|
65 | public function inversedBy($fieldName) |
---|
66 | { |
---|
67 | $this->mapping['inversedBy'] = $fieldName; |
---|
68 | return $this; |
---|
69 | } |
---|
70 | |
---|
71 | public function cascadeAll() |
---|
72 | { |
---|
73 | $this->mapping['cascade'] = array("ALL"); |
---|
74 | return $this; |
---|
75 | } |
---|
76 | |
---|
77 | public function cascadePersist() |
---|
78 | { |
---|
79 | $this->mapping['cascade'][] = "persist"; |
---|
80 | return $this; |
---|
81 | } |
---|
82 | |
---|
83 | public function cascadeRemove() |
---|
84 | { |
---|
85 | $this->mapping['cascade'][] = "remove"; |
---|
86 | return $this; |
---|
87 | } |
---|
88 | |
---|
89 | public function cascadeMerge() |
---|
90 | { |
---|
91 | $this->mapping['cascade'][] = "merge"; |
---|
92 | return $this; |
---|
93 | } |
---|
94 | |
---|
95 | public function cascadeDetach() |
---|
96 | { |
---|
97 | $this->mapping['cascade'][] = "detach"; |
---|
98 | return $this; |
---|
99 | } |
---|
100 | |
---|
101 | public function cascadeRefresh() |
---|
102 | { |
---|
103 | $this->mapping['cascade'][] = "refresh"; |
---|
104 | return $this; |
---|
105 | } |
---|
106 | |
---|
107 | public function fetchExtraLazy() |
---|
108 | { |
---|
109 | $this->mapping['fetch'] = ClassMetadata::FETCH_EXTRA_LAZY; |
---|
110 | return $this; |
---|
111 | } |
---|
112 | |
---|
113 | public function fetchEager() |
---|
114 | { |
---|
115 | $this->mapping['fetch'] = ClassMetadata::FETCH_EAGER; |
---|
116 | return $this; |
---|
117 | } |
---|
118 | |
---|
119 | public function fetchLazy() |
---|
120 | { |
---|
121 | $this->mapping['fetch'] = ClassMetadata::FETCH_LAZY; |
---|
122 | return $this; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Add Join Columns |
---|
127 | * |
---|
128 | * @param string $columnName |
---|
129 | * @param string $referencedColumnName |
---|
130 | * @param bool $nullable |
---|
131 | * @param bool $unique |
---|
132 | * @param string $onDelete |
---|
133 | * @param string $columnDef |
---|
134 | */ |
---|
135 | public function addJoinColumn($columnName, $referencedColumnName, $nullable = true, $unique = false, $onDelete = null, $columnDef = null) |
---|
136 | { |
---|
137 | $this->joinColumns[] = array( |
---|
138 | 'name' => $columnName, |
---|
139 | 'referencedColumnName' => $referencedColumnName, |
---|
140 | 'nullable' => $nullable, |
---|
141 | 'unique' => $unique, |
---|
142 | 'onDelete' => $onDelete, |
---|
143 | 'columnDefinition' => $columnDef, |
---|
144 | ); |
---|
145 | return $this; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * @return ClassMetadataBuilder |
---|
150 | */ |
---|
151 | public function build() |
---|
152 | { |
---|
153 | $mapping = $this->mapping; |
---|
154 | if ($this->joinColumns) { |
---|
155 | $mapping['joinColumns'] = $this->joinColumns; |
---|
156 | } |
---|
157 | $cm = $this->builder->getClassMetadata(); |
---|
158 | if ($this->type == ClassMetadata::MANY_TO_ONE) { |
---|
159 | $cm->mapManyToOne($mapping); |
---|
160 | } else if ($this->type == ClassMetadata::ONE_TO_ONE) { |
---|
161 | $cm->mapOneToOne($mapping); |
---|
162 | } else { |
---|
163 | throw new \InvalidArgumentException("Type should be a ToOne Assocation here"); |
---|
164 | } |
---|
165 | return $this->builder; |
---|
166 | } |
---|
167 | } |
---|