1 | <?php |
||
48 | class Table |
||
49 | { |
||
50 | /** |
||
51 | * Table name |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $name; |
||
56 | |||
57 | /** |
||
58 | * Connection |
||
59 | * |
||
60 | * @var Connection |
||
61 | */ |
||
62 | protected $connection; |
||
63 | |||
64 | /** |
||
65 | * Array of primary keys |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $identifiersKey; |
||
70 | |||
71 | /** |
||
72 | * Entity registry |
||
73 | * |
||
74 | * @var Registry |
||
75 | */ |
||
76 | protected $registry; |
||
77 | |||
78 | /** |
||
79 | * Classname of the default entity for this table |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $defaultEntity; |
||
84 | |||
85 | /** |
||
86 | * List of default entity listeners to be used with this table |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $defaultEntityListeners = array(); |
||
91 | |||
92 | /** |
||
93 | * Constructor |
||
94 | * |
||
95 | * @param string $tableName This table name |
||
96 | * |
||
97 | * @return void |
||
|
|||
98 | */ |
||
99 | public function __construct($tableName) |
||
103 | |||
104 | /** |
||
105 | * Returns all columns from this table |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getColumns() |
||
117 | |||
118 | /** |
||
119 | * Defines a connection for this table |
||
120 | * |
||
121 | * @param Connection $connection Database connection |
||
122 | * |
||
123 | * @return Table |
||
124 | */ |
||
125 | public function setConnection(Connection $connection) |
||
131 | |||
132 | /** |
||
133 | * Returns current connection for this table |
||
134 | * |
||
135 | * @throws Exception If no connection is defined |
||
136 | * @return Connection |
||
137 | */ |
||
138 | public function getConnection() |
||
151 | |||
152 | /** |
||
153 | * Returns a Finder instance to navigate into this table |
||
154 | * |
||
155 | * @param string $entity Entity class to be returned by this Finder |
||
156 | * @param array $listeners Overrides default entity/table listeners |
||
157 | * |
||
158 | * @return Finder |
||
159 | */ |
||
160 | public function finder($entity = null, array $listeners = array()) |
||
168 | |||
169 | /** |
||
170 | * Returns this table's name |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getName() |
||
179 | |||
180 | /** |
||
181 | * Returns primary identifiers keys for this table |
||
182 | * |
||
183 | * @throws Exceptions\TableLacksIdentifiersException |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | public function getIdentifiersKeys() |
||
188 | { |
||
189 | if (!isset($this->identifiersKey)) { |
||
190 | $idx = $this |
||
191 | ->getConnection() |
||
192 | ->getSchema() |
||
193 | ->getTable($this->name) |
||
194 | ->getIndexes(); |
||
195 | |||
196 | if (!count($idx)) { |
||
197 | throw new Exceptions\TableLacksIdentifiersException( |
||
198 | sprintf('"%s" has no indexes', $this->name) |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | $final = array(); |
||
203 | foreach ($idx as $index) { |
||
204 | if ($index->isPrimary()) { |
||
205 | $final += $index->getColumns(); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | $final = array_unique($final); |
||
210 | if (!count($final)) { |
||
211 | throw new Exceptions\TableLacksIdentifiersException( |
||
212 | sprintf('"%s" has no identifiers key(s)', $this->name) |
||
213 | ); |
||
214 | } |
||
215 | |||
216 | $this->identifiersKey = $final; |
||
217 | } |
||
218 | |||
219 | return $this->identifiersKey; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Returns the entity registry for this table |
||
224 | * |
||
225 | * @return Registry |
||
226 | */ |
||
227 | public function getRegistry() |
||
235 | |||
236 | /** |
||
237 | * Returns an object representation of a given column |
||
238 | * |
||
239 | * @param string $columnName Column name |
||
240 | * |
||
241 | * @return \Doctrine\DBAL\Schema\Column |
||
242 | */ |
||
243 | public function getColumn($columnName) |
||
260 | |||
261 | /** |
||
262 | * Tells if this table has a column named $columnName |
||
263 | * |
||
264 | * @param string $columnName Column name |
||
265 | * |
||
266 | * @return boolean |
||
267 | */ |
||
268 | public function hasColumn($columnName) |
||
274 | |||
275 | /** |
||
276 | * Defines the default returned entity |
||
277 | * |
||
278 | * @param string $entityClass Entity class name |
||
279 | * |
||
280 | * @return Table |
||
281 | */ |
||
282 | public function setDefaultEntity($entityClass) |
||
288 | |||
289 | /** |
||
290 | * Returns the default returned entity for this table |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public function getDefaultEntity() |
||
302 | |||
303 | /** |
||
304 | * Defines the default entity listeners to be used with this table |
||
305 | * |
||
306 | * @param array $listeners List of listeners |
||
307 | * |
||
308 | * @return Table |
||
309 | */ |
||
310 | public function setDefaultEntityListeners(array $listeners) |
||
316 | |||
317 | /** |
||
318 | * Returns the default entity listeners used this table |
||
319 | * |
||
320 | * @return array |
||
321 | */ |
||
322 | public function getDefaultEntityListeners() |
||
326 | |||
327 | /** |
||
328 | * Save one or more entities into this table |
||
329 | * |
||
330 | * @param mixed $entity Entity or List of entities |
||
331 | * @param array $listeners Overrides default entity/table listeners |
||
332 | * |
||
333 | * @return void |
||
334 | */ |
||
335 | public function save($entity, array $listeners = array()) |
||
359 | |||
360 | /** |
||
361 | * Delete one or more entities from this table |
||
362 | * |
||
363 | * @param mixed $entity Entity or List of entities |
||
364 | * @param array $listeners Overrides default entity listeners |
||
365 | * |
||
366 | * @return void |
||
367 | */ |
||
368 | public function delete($entity, array $listeners = array()) |
||
392 | |||
393 | /** |
||
394 | * Executes all waiting workers |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | protected function work() |
||
407 | |||
408 | /** |
||
409 | * Delete all entries from this table |
||
410 | * |
||
411 | * @return void |
||
412 | */ |
||
413 | public function deleteAll() |
||
418 | } |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.