/
Association.php
408 lines (369 loc) · 10.6 KB
/
Association.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
<?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;
/**
* An Association is a relationship established between two tables and is used
* to configure and customize the way interconnected records are retrieved.
*
*/
abstract class Association {
/**
* Strategy name to use joins for fetching associated records
*
* @var string
*/
const STRATEGY_JOIN = 'join';
/**
* Strategy name to use a subquery for fetching associated records
*
* @var string
*/
const STRATEGY_SUBQUERY = 'subquery';
/**
* Strategy name to use a select for fetching associated records
*
* @var string
*/
const STRATEGY_SELECT = 'select';
/**
* Name given to the association, it usually represents the alias
* assigned to the target associated table
*
* @var string
*/
protected $_name;
/**
* Whether this association can be expressed directly in a query join
*
* @var boolean
*/
protected $_canBeJoined = false;
/**
* The className of the target table object
*
* @var string
*/
protected $_className;
/**
* The name of the field representing the foreign key to the target table
*
* @var string
*/
protected $_foreignKey;
/**
* A list of conditions to be always included when fetching records from
* the target association
*
* @var array
*/
protected $_conditions = [];
/**
* Whether the records on the target table are dependent on the source table,
* often used to indicate that records should be removed is the owning record in
* the source table is deleted.
*
* @var boolean
*/
protected $_dependent = false;
/**
* Source table instance
*
* @var Cake\ORM\Table
*/
protected $_sourceTable;
/**
* Target table instance
*
* @var Cake\ORM\Table
*/
protected $_targetTable;
/**
* The type of join to be used when adding the association to a query
*
* @var string
*/
protected $_joinType = 'LEFT';
/**
* The property name that should be filled with data from the target table
* in the source table record.
*
* @var string
*/
protected $_property;
/**
* The strategy name to be used to fetch associated records. Some association
* types might not implement but one strategy to fetch records.
*
* @var string
*/
protected $_strategy = self::STRATEGY_JOIN;
/**
* Constructor. Subclasses can override _options function to get the original
* list of passed options if expecting any other special key
*
* @param string $name The name given to the association
* @param array $options A list of properties to be set on this object
* @return void
*/
public function __construct($name, array $options = []) {
$defaults = [
'className',
'foreignKey',
'conditions',
'dependent',
'sourceTable',
'targetTable',
'joinType',
'property'
];
foreach ($defaults as $property) {
if (isset($options[$property])) {
$this->{'_' . $property} = $options[$property];
}
}
$this->_name = $name;
$this->_options($options);
if (empty($this->_property)) {
$this->property($name);
}
if (!empty($options['strategy'])) {
$this->strategy($options['strategy']);
}
}
/**
* Sets the name for this association. If no argument is passed then the current
* configured name will be returned
*
* @param string $name Name to be assigned
* @return string
*/
public function name($name = null) {
if ($name !== null) {
$this->_name = $name;
}
return $this->_name;
}
/**
* Sets the table instance for the source side of the association. If no arguments
* are passed, the current configured table instance is returned
*
* @param Cake\ORM\Table $table the instance to be assigned as source side
* @return Cake\ORM\Table
*/
public function source(Table $table = null) {
if ($table === null) {
return $this->_sourceTable;
}
return $this->_sourceTable = $table;
}
/**
* Sets the table instance for the target side of the association. If no arguments
* are passed, the current configured table instance is returned
*
* @param Cake\ORM\Table $table the instance to be assigned as target side
* @return Cake\ORM\Table
*/
public function target(Table $table = null) {
if ($table === null && $this->_targetTable) {
return $this->_targetTable;
}
if ($table !== null) {
return $this->_targetTable = $table;
}
if ($table === null) {
$className = $this->_className;
$this->_targetTable = Table::build($this->_name, compact('className'));
}
return $this->_targetTable;
}
/**
* Sets a list of conditions to be always included when fetching records from
* the target association. If no parameters are passed current list is returned
*
* @param array $conditions list of conditions to be used
* @see Cake\Database\Query::where() for examples on the format of the array
* @return array
*/
public function conditions($conditions = null) {
if ($conditions !== null) {
$this->_conditions = $conditions;
}
return $this->_conditions;
}
/**
* Sets the name of the field representing the foreign key to the target table.
* If no parameters are passed current field is returned
*
* @param string $key the key to be used to link both tables together
* @return string
*/
public function foreignKey($key = null) {
if ($key !== null) {
$this->_foreignKey = $key;
}
return $this->_foreignKey;
}
/**
* Sets Whether the records on the target table are dependent on the source table,
* often used to indicate that records should be removed is the owning record in
* the source table is deleted.
* If no parameters are passed current setting is returned.
*
* @param boolean $dependent
* @return boolean
*/
public function dependent($dependent = null) {
if ($dependent !== null) {
$this->_dependent = $dependent;
}
return $this->_dependent;
}
/**
* Whether this association can be expressed directly in a query join
*
* @param array $options custom options key that could alter the return value
* @return boolean
*/
public function canBeJoined($options = []) {
return $this->_canBeJoined;
}
/**
* Sets the type of join to be used when adding the association to a query.
* If no arguments are passed, currently configured type is returned.
*
* @param string $type the join type to be used (e.g. INNER)
* @return string
*/
public function joinType($type = null) {
if ($type === null) {
return $this->_joinType;
}
return $this->_joinType = $type;
}
/**
* Sets the property name that should be filled with data from the target table
* in the source table record.
* If no arguments are passed, currently configured type is returned.
*
* @param string $name
* @return string
*/
function property($name = null) {
if ($name !== null) {
$this->_property = $name;
}
return $this->_property;
}
/**
* Sets the strategy name to be used to fetch associated records. Keep in mind
* that some association types might not implement but a default strategy,
* rendering any changes to this setting void.
* If no arguments are passed, currently configured strategy is returned.
*
* @param string $name
* @return string
*/
function strategy($name = null) {
if ($name !== null) {
$valid = [self::STRATEGY_JOIN, self::STRATEGY_SELECT, self::STRATEGY_SUBQUERY];
if (!in_array($name, $valid)) {
throw new \InvalidArgumentException(
sprintf(__d('cake_dev', 'Invalid strategy "%s" was provided'), $name)
);
}
$this->_strategy = $name;
}
return $this->_strategy;
}
/**
* Override this function to initialize any concrete association class, it will
* get passed the original list of options used in the constructor
*
* @param array $options List of options used for initialization
* @return void
*/
protected function _options(array $options) {
}
/**
* Alters a Query object to include the associated target table data in the final
* result
*
* The options array accept the following keys:
*
* - includeFields: Whether to include target model fields in the result or not
* - 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, this
* will be merged with any conditions originally configured for this association
* - fields: a list of fields in the target table to include in the result
* - type: The type of join to be used (e.g. INNER)
*
* @param Query $query the query to be altered to include the target table data
* @param array $options Any extra options or overrides to be taken in account
* @return void
*/
public function attachTo(Query $query, array $options = []) {
$target = $this->target();
$source = $this->source();
$options += [
'includeFields' => true,
'foreignKey' => $this->foreignKey(),
'conditions' => [],
'type' => $this->joinType(),
'table' => $target->table()
];
$options['conditions'] = array_merge($this->conditions(), $options['conditions']);
if (!empty($options['foreignKey'])) {
$joinCondition = $this->_joinCondition($options);
if ($joinCondition) {
$options['conditions'][] = $joinCondition;
}
}
$joinOptions = ['table' => 1, 'conditions' => 1, 'type' => 1];
$query->join([$this->_name => array_intersect_key($options, $joinOptions)]);
if (empty($options['fields'])) {
$f = isset($options['fields']) ? $options['fields'] : null;
if ($options['includeFields'] && ($f === null || $f !== false)) {
$options['fields'] = $target->schema()->columns();
}
}
if (!empty($options['fields'])) {
$query->select($query->aliasFields($options['fields'], $this->_name));
}
}
/**
* Correctly nests a result row associated values into the correct array keys inside the
* source results.
*
* @param array $result
* @return array
*/
public function transformRow($row) {
$sourceAlias = $this->source()->alias();
$row[$sourceAlias][$this->property()] = $row[$this->_name];
return $row;
}
/**
* Returns a single or multiple conditions to be appended to the generated join
* clause for getting the results on the target table. If false is returned then
* it will not attach any new conditions to the join clause
*
* @param array $options list of options passed to attachTo method
* @return string|array|boolean
*/
protected abstract function _joinCondition(array $options);
}