/
Table.php
490 lines (455 loc) · 15.1 KB
/
Table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\ORM;
use Cake\Database\Schema\Table as Schema;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Association\HasMany;
use Cake\ORM\Association\HasOne;
use Cake\Utility\Inflector;
/**
* Represents a single database table. Exposes methods for retrieving data out
* of it and manages the associations it has to other tables. Multiple
* instances of this class can be created for the same database table with
* different aliases, this allows you to address your database structure in a
* richer and more expressive way.
*/
class Table {
/**
* A list of all table instances that has been built using the factory
* method. Instances are indexed by alias
*
* @var array
*/
protected static $_instances = [];
/**
* A collection of default options to apply to each table built with the
* factory method. Indexed by table name
*
* @var array
*/
protected static $_tablesMap = [];
/**
* Name of the table as it can be found in the database
*
* @var string
*/
protected $_table;
/**
* Human name giving to this particular instance. Multiple objects representing
* the same database table can exist by using different aliases.
*
* @var string
*/
protected $_alias;
/**
* Connection instance
*
* @var \Cake\Database\Connection
*/
protected $_connection;
/**
* The schema object containing a description of this table fields
*
* @var \Cake\Database\Schema\Table
*/
protected $_schema;
/**
* The name of the field that represents the primary key in the table
*
* @var string
*/
protected $_primaryKey = 'id';
/**
* The list of associations for this table. Indexed by association name,
* values are Association object instances.
*
* @var array
*/
protected $_associations = [];
/**
* Initializes a new instance
*
* The $config array understands the following keys:
*
* - table: Name of the database table to represent
* - alias: Alias to be assigned to this table (default to table name)
* - connection: The connection instance to use
* - schema: A \Cake\Database\Schema\Table object or an array that can be
* passed to it.
*
* @param array config Lsit of options for this table
* @return void
*/
public function __construct($config = array()) {
if (!empty($config['table'])) {
$this->table($config['table']);
}
if (!empty($config['alias'])) {
$this->alias($config['alias']);
}
$table = $this->table();
if (isset(static::$_tablesMap[$table])) {
$config = array_merge(static::$_tablesMap[$table], $config);
}
if (!empty($config['connection'])) {
$this->connection($config['connection']);
}
if (!empty($config['schema'])) {
$this->schema($config['schema']);
}
}
/**
* A factory method to build a new Table instance. Once created, the instance
* will be registered so it can be re-used if tried to build again for the same
* alias.
*
* The options that can be passed are the same as in `__construct()`, but the
* key `className` is also recognized.
*
* When $options contains `className` this method will try to instantiate an
* object of that class instead of this default Table class.
*
* If no `table` option is passed, the table name will be the tableized version
* of the provided $alias.
*
* @param string $alias the alias for the table
* @param array $options a list of options as accepted by `__construct()`
* @return Table
*/
public static function build($alias, array $options = []) {
if (isset(static::$_instances[$alias])) {
return static::$_instances[$alias];
}
$options = ['alias' => $alias] + $options;
if (empty($options['className'])) {
$options['className'] = get_called_class();
}
return static::$_instances[$alias] = new $options['className']($options);
}
/**
* Returns the Table object associated to the provided alias, if any.
* If a Table is passed as second parameter it will be associated to the
* passed $alias even if another object was associated before.
*
* @param string $alias
* @param Table $object
* @return Table
*/
public static function instance($alias, self $object = null) {
if ($object === null) {
return isset(static::$_instances[$alias]) ? static::$_instances[$alias] : null;
}
return static::$_instances[$alias] = $object;
}
/**
* Stores a list of options to be used when instantiating an object for the table
* with the same name as $table. The options that can be stored are those that
* are recognized by `build()`
*
* If second argument is omitted, it will return the current settings for $table
*
* If no arguments are passed it will return the full configuration array for
* all tables
*
* @param string $table name of the table
* @param array $options list of options for the table
* @return array
*/
public static function config($table = null, array $options = null) {
if ($table === null) {
return static::$_tablesMap;
}
if (!is_string($table)) {
return static::$_tablesMap = $table;
}
if ($options === null) {
return isset(static::$_tablesMap[$table]) ? static::$_tablesMap[$table] : [];
}
return static::$_tablesMap[$table] = $options;
}
/**
* Clears the registry of instantiated tables and default configurations
*
* @return void
*/
public static function clearRegistry() {
static::$_instances = [];
static::$_tablesMap = [];
}
/**
* Returns the database table name or sets a new one
*
* @param string $table the new table name
* @return string
*/
public function table($table = null) {
if ($table !== null) {
$this->_table = $table;
}
if ($this->_table === null) {
$table = explode('\\', get_class($this));
$table = substr(end($table), 0, -5);
if (empty($table)) {
$table = $this->alias();
}
$this->_table = Inflector::tableize($table);
}
return $this->_table;
}
/**
* Returns the table alias or sets a new one
*
* @param string $table the new table alias
* @return string
*/
public function alias($alias = null) {
if ($alias !== null) {
$this->_alias = $alias;
}
if ($this->_alias === null) {
$alias = explode('\\', get_class($this));
$alias = substr(end($alias), 0, -5) ?: $this->_table;
$this->_alias = $alias;
}
return $this->_alias;
}
/**
* Returns the connection instance or sets a new one
*
* @param \Cake\Database\Connection $conn the new connection instance
* @return \Cake\Database\Connection
*/
public function connection($conn = null) {
if ($conn === null) {
return $this->_connection;
}
return $this->_connection = $conn;
}
/**
* Returns the schema table object describing this table's properties.
*
* If an \Cake\Database\Schema\Table is passed, it will be used for this table
* instead of the default one.
*
* If an array is passed, a new \Cake\Database\Schema\Table will be constructed
* out of it and used as the schema for this table.
*
* @param array|\Cake\Database\Schema\Table new schema to be used for this table
* @return \Cake\Database\Schema\Table
*/
public function schema($schema = null) {
if ($schema === null) {
if ($this->_schema === null) {
$this->_schema = $this->connection()
->schemaCollection()
->describe($this->_table);
}
return $this->_schema;
}
if (is_array($schema)) {
$schema = new Schema($this->table(), $schema);
}
return $this->_schema = $schema;
}
/**
* Returns the primary key field name or sets a new one
*
* @param string $key sets a new name to be used as primary key
* @return string
*/
public function primaryKey($key = null) {
if ($key !== null) {
$this->_primaryKey = $key;
}
return $this->_primaryKey;
}
/**
* Returns a association objected configured for the specified alias if any
*
* @param string $name the alias used for the association
* @return Cake\ORM\Association
*/
public function association($name) {
if (isset($this->_associations[$name])) {
return $this->_associations[$name];
}
return null;
}
/**
* Creates a new BelongsTo association between this table and a target
* table. A "belongs to" association is a N-1 relationship where this table
* is the N side, and where there is a single associated record in the target
* table for each one in this table.
*
* Target table can be inferred by its name, which is provided in the
* first argument, or you can either pass the to be instantiated or
* an instance of it directly.
*
* The options array accept the following keys:
*
* - className: The class name of the target table object
* - targetTable: An instance of a table object to be used as the target table
* - foreignKey: The name of the field to use as foreign key, if false none
* will be used
* - conditions: array with a list of conditions to filter the join with
* - joinType: The type of join to be used (e.g. INNER)
*
* This method will return the association object that was built.
*
* @param string $associated the alias for the target table. This is used to
* uniquely identify the association
* @param array $options list of options to configure the association definition
* @return Cake\ORM\Association\BelongsTo
*/
public function belongsTo($associated, array $options = []) {
$options += ['sourceTable' => $this];
$association = new BelongsTo($associated, $options);
return $this->_associations[$association->name()] = $association;
}
/**
* Creates a new HasOne association between this table and a target
* table. A "has one" association is a 1-1 relationship.
*
* Target table can be inferred by its name, which is provided in the
* first argument, or you can either pass the class name to be instantiated or
* an instance of it directly.
*
* The options array accept the following keys:
*
* - className: The class name of the target table object
* - targetTable: An instance of a table object to be used as the target table
* - foreignKey: The name of the field to use as foreign key, if false none
* will be used
* - conditions: array with a list of conditions to filter the join with
* - joinType: The type of join to be used (e.g. LEFT)
*
* This method will return the association object that was built.
*
* @param string $associated the alias for the target table. This is used to
* uniquely identify the association
* @param array $options list of options to configure the association definition
* @return Cake\ORM\Association\HasOne
*/
public function hasOne($associated, array $options = []) {
$options += ['sourceTable' => $this];
$association = new HasOne($associated, $options);
return $this->_associations[$association->name()] = $association;
}
/**
* Creates a new HasMany association between this table and a target
* table. A "has many" association is a 1-N relationship.
*
* Target table can be inferred by its name, which is provided in the
* first argument, or you can either pass the class name to be instantiated or
* an instance of it directly.
*
* The options array accept the following keys:
*
* - className: The class name of the target table object
* - targetTable: An instance of a table object to be used as the target table
* - foreignKey: The name of the field to use as foreign key, if false none
* will be used
* - conditions: array with a list of conditions to filter the join with
* - sort: The order in which results for this association should be returned
* - strategy: The strategy to be used for selecting results Either 'select'
* or 'subquery'. If subquery is selected the query used to return results
* in the source table will be used as conditions for getting rows in the
* target table.
*
* This method will return the association object that was built.
*
* @param string $associated the alias for the target table. This is used to
* uniquely identify the association
* @param array $options list of options to configure the association definition
* @return Cake\ORM\Association\HasMany
*/
public function hasMany($associated, array $options = []) {
$options += ['sourceTable' => $this];
$association = new HasMany($associated, $options);
return $this->_associations[$association->name()] = $association;
}
/**
* Creates a new BelongsToMany association between this table and a target
* table. A "belongs to many" association is a M-N relationship.
*
* Target table can be inferred by its name, which is provided in the
* first argument, or you can either pass the class name to be instantiated or
* an instance of it directly.
*
* The options array accept the following keys:
*
* - className: The class name of the target table object
* - targetTable: An instance of a table object to be used as the target table
* - foreignKey: The name of the field to use as foreign key
* - joinTable: The name of the table representing the link between the two
* - through: If you choose to use an already instantiated link table, set this
* key to a configured Table instance containing associations to both the source
* and target tables in this association.
* - conditions: array with a list of conditions to filter the join with
* - sort: The order in which results for this association should be returned
* - strategy: The strategy to be used for selecting results Either 'select'
* or 'subquery'. If subquery is selected the query used to return results
* in the source table will be used as conditions for getting rows in the
* target table.
*
* This method will return the association object that was built.
*
* @param string $associated the alias for the target table. This is used to
* uniquely identify the association
* @param array $options list of options to configure the association definition
* @return Cake\ORM\Association\BelongsToMany
*/
public function belongsToMany($associated, array $options = []) {
$options += ['sourceTable' => $this];
$association = new BelongsToMany($associated, $options);
return $this->_associations[$association->name()] = $association;
}
/**
* Creates a new Query for this table and applies some defaults based on the
* type of search that was selected
*
* @param string $type the type of query to perform
* @param array $options
* @return \Cake\ORM\Query
*/
public function find($type, $options = []) {
return $this->{'find' . ucfirst($type)}($this->buildQuery(), $options);
}
/**
* Applies the options required to find all records on this table based
* on the passed options.
*
* @param \Cake\ORM\Query $query
* @param array $options
* @return \Cake\ORM\Query
*/
public function findAll(Query $query, array $options = []) {
return $query;
}
/**
* Creates a new Query instance for this table
*
* @return \Cake\ORM\Query
*/
protected function buildQuery() {
$query = new Query($this->connection());
return $query
->repository($this)
->select();
}
}