/
ExternalAssociationTrait.php
126 lines (111 loc) · 3.31 KB
/
ExternalAssociationTrait.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
<?php
/**
* 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 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM\Association;
/**
* Represents a type of association that that needs to be recovered by performing
* an extra query.
*/
trait ExternalAssociationTrait
{
use SelectableAssociationTrait {
_defaultOptions as private _selectableOptions;
}
/**
* Order in which target records should be returned
*
* @var mixed
*/
protected $_sort;
/**
* Whether this association can be expressed directly in a query join
*
* @param array $options custom options key that could alter the return value
* @return bool if the 'matching' key in $option is true then this function
* will return true, false otherwise
*/
public function canBeJoined(array $options = [])
{
return !empty($options['matching']);
}
/**
* Sets the name of the field representing the foreign key to the source table.
* If no parameters are passed current field is returned
*
* @param string|null $key the key to be used to link both tables together
* @return string
*/
public function foreignKey($key = null)
{
if ($key === null) {
if ($this->_foreignKey === null) {
$this->_foreignKey = $this->_modelKey($this->source()->table());
}
return $this->_foreignKey;
}
return parent::foreignKey($key);
}
/**
* Sets the sort order in which target records should be returned.
* If no arguments are passed the currently configured value is returned
*
* @param mixed $sort A find() compatible order clause
* @return mixed
*/
public function sort($sort = null)
{
if ($sort !== null) {
$this->_sort = $sort;
}
return $this->_sort;
}
/**
* {@inheritDoc}
*/
public function defaultRowValue($row, $joined)
{
$sourceAlias = $this->source()->alias();
if (isset($row[$sourceAlias])) {
$row[$sourceAlias][$this->property()] = $joined ? null : [];
}
return $row;
}
/**
* Returns the default options to use for the eagerLoader
*
* @return array
*/
protected function _defaultOptions()
{
return $this->_selectableOptions() + [
'sort' => $this->sort()
];
}
/**
* {@inheritDoc}
*/
protected function _buildResultMap($fetchQuery, $options)
{
$resultMap = [];
$key = (array)$options['foreignKey'];
foreach ($fetchQuery->all() as $result) {
$values = [];
foreach ($key as $k) {
$values[] = $result[$k];
}
$resultMap[implode(';', $values)][] = $result;
}
return $resultMap;
}
}