/
BelongsTo.php
156 lines (142 loc) · 4.92 KB
/
BelongsTo.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
<?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 MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\ORM\Association;
use Cake\Database\Expression\IdentifierExpression;
use Cake\ORM\Association;
use Cake\ORM\Entity;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
/**
* Represents an 1 - N relationship where the source side of the relation is
* related to only one record in the target table.
*
* An example of a BelongsTo association would be Article belongs to Author.
*/
class BelongsTo extends Association {
/**
* 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) {
if ($this->_foreignKey === null) {
$key = Inflector::singularize($this->target()->alias());
$this->_foreignKey = Inflector::underscore($key) . '_id';
}
return $this->_foreignKey;
}
return parent::foreignKey($key);
}
/**
* Handle cascading deletes.
*
* BelongsTo associations are never cleared in a cascading delete scenario.
*
* @param \Cake\ORM\Entity $entity The entity that started the cascaded delete.
* @param array $options The options for the original delete.
* @return boolean Success.
*/
public function cascadeDelete(Entity $entity, $options = []) {
return true;
}
/**
* 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
*/
public function property($name = null) {
if ($name !== null) {
return parent::property($name);
}
if ($name === null && !$this->_propertyName) {
list($plugin, $name) = pluginSplit($this->_name);
$this->_propertyName = Inflector::underscore(Inflector::singularize($name));
}
return $this->_propertyName;
}
/**
* Returns whether or not the passed table is the owning side for this
* association. This means that rows in the 'target' table would miss important
* or required information if the row in 'source' did not exist.
*
* @param \Cake\ORM\Table $side The potential Table with ownership
* @return boolean
*/
public function isOwningSide(Table $side) {
return $side === $this->target();
}
/**
* Takes an entity from the source table and looks if there is a field
* matching the property name for this association. The found entity will be
* saved on the target table for this association by passing supplied
* `$options`
*
* @param \Cake\ORM\Entity $entity an entity from the source table
* @param array|\ArrayObject $options options to be passed to the save method in
* the target table
* @return boolean|Entity false if $entity could not be saved, otherwise it returns
* the saved entity
* @see Table::save()
*/
public function save(Entity $entity, $options = []) {
$targetEntity = $entity->get($this->property());
if (empty($targetEntity) || !($targetEntity instanceof Entity)) {
return $entity;
}
$table = $this->target();
$targetEntity = $table->save($targetEntity, $options);
if (!$targetEntity) {
return false;
}
$properties = array_combine(
(array)$this->foreignKey(),
$targetEntity->extract((array)$table->primaryKey())
);
$entity->set($properties, ['guard' => false]);
return $entity;
}
/**
* Returns a single or multiple conditions to be appended to the generated join
* clause for getting the results on the target table.
*
* @param array $options list of options passed to attachTo method
* @return array
* @throws \RuntimeException if the number of columns in the foreignKey do not
* match the number of columns in the target table primaryKey
*/
protected function _joinCondition(array $options) {
$conditions = [];
$tAlias = $this->target()->alias();
$sAlias = $this->_sourceTable->alias();
$foreignKey = (array)$options['foreignKey'];
$primaryKey = (array)$this->_targetTable->primaryKey();
if (count($foreignKey) !== count($primaryKey)) {
$msg = 'Cannot match provided foreignKey, got %d columns expected %d';
throw new \RuntimeException(sprintf($msg, count($foreignKey), count($primaryKey)));
}
foreach ($foreignKey as $k => $f) {
$field = sprintf('%s.%s', $tAlias, $primaryKey[$k]);
$value = new IdentifierExpression(sprintf('%s.%s', $sAlias, $f));
$conditions[$field] = $value;
}
return $conditions;
}
}