|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Subclass of {@link DataList} representing a has_many relation. |
|
9
|
|
|
*/ |
|
10
|
|
|
class HasManyList extends RelationList |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $foreignKey; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Create a new HasManyList object. |
|
20
|
|
|
* Generation of the appropriate record set is left up to the caller, using the normal |
|
21
|
|
|
* {@link DataList} methods. Addition arguments are used to support {@@link add()} |
|
22
|
|
|
* and {@link remove()} methods. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $dataClass The class of the DataObjects that this will list. |
|
25
|
|
|
* @param string $foreignKey The name of the foreign key field to set the ID filter against. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($dataClass, $foreignKey) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($dataClass); |
|
30
|
|
|
|
|
31
|
|
|
$this->foreignKey = $foreignKey; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Gets the field name which holds the related object ID. |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getForeignKey() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->foreignKey; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param null|int|array|string $id |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function foreignIDFilter($id = null) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($id === null) { |
|
51
|
|
|
$id = $this->getForeignID(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Apply relation filter |
|
55
|
|
|
$key = DataObject::getSchema()->sqlColumnForField($this->dataClass(), $this->getForeignKey()); |
|
56
|
|
|
if (is_array($id)) { |
|
57
|
|
|
return ["$key IN (" . DB::placeholders($id) . ")" => $id]; |
|
58
|
|
|
} |
|
59
|
|
|
if ($id !== null) { |
|
60
|
|
|
return [$key => $id]; |
|
61
|
|
|
} |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Adds the item to this relation. |
|
67
|
|
|
* |
|
68
|
|
|
* It does so by setting the relationFilters. |
|
69
|
|
|
* |
|
70
|
|
|
* @param DataObject|int $item The DataObject to be added, or its ID |
|
71
|
|
|
*/ |
|
72
|
|
|
public function add($item) |
|
73
|
|
|
{ |
|
74
|
|
|
if (is_numeric($item)) { |
|
75
|
|
|
$item = DataObject::get_by_id($this->dataClass, $item); |
|
76
|
|
|
} elseif (!($item instanceof $this->dataClass)) { |
|
77
|
|
|
user_error("HasManyList::add() expecting a $this->dataClass object, or ID value", E_USER_ERROR); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$foreignID = $this->getForeignID(); |
|
81
|
|
|
|
|
82
|
|
|
// Validate foreignID |
|
83
|
|
|
if (!$foreignID) { |
|
84
|
|
|
user_error("HasManyList::add() can't be called until a foreign ID is set", E_USER_WARNING); |
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
if (is_array($foreignID)) { |
|
|
|
|
|
|
88
|
|
|
user_error("HasManyList::add() can't be called on a list linked to mulitple foreign IDs", E_USER_WARNING); |
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$foreignKey = $this->foreignKey; |
|
93
|
|
|
$item->$foreignKey = $foreignID; |
|
94
|
|
|
|
|
95
|
|
|
$item->write(); |
|
96
|
|
|
|
|
97
|
|
|
if ($this->addCallbacks) { |
|
98
|
|
|
$this->addCallbacks->call($this, $item, []); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Remove an item from this relation. |
|
104
|
|
|
* |
|
105
|
|
|
* Doesn't actually remove the item, it just clears the foreign key value. |
|
106
|
|
|
* |
|
107
|
|
|
* @param int $itemID The ID of the item to be removed. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function removeByID($itemID) |
|
110
|
|
|
{ |
|
111
|
|
|
$item = $this->byID($itemID); |
|
112
|
|
|
|
|
113
|
|
|
return $this->remove($item); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Remove an item from this relation. |
|
118
|
|
|
* Doesn't actually remove the item, it just clears the foreign key value. |
|
119
|
|
|
* |
|
120
|
|
|
* @param DataObject $item The DataObject to be removed |
|
121
|
|
|
* @todo Maybe we should delete the object instead? |
|
122
|
|
|
*/ |
|
123
|
|
|
public function remove($item) |
|
124
|
|
|
{ |
|
125
|
|
|
if (!($item instanceof $this->dataClass)) { |
|
126
|
|
|
throw new InvalidArgumentException( |
|
127
|
|
|
"HasManyList::remove() expecting a $this->dataClass object, or ID", |
|
128
|
|
|
E_USER_ERROR |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// Don't remove item which doesn't belong to this list |
|
133
|
|
|
$foreignID = $this->getForeignID(); |
|
134
|
|
|
$foreignKey = $this->getForeignKey(); |
|
135
|
|
|
|
|
136
|
|
|
if (empty($foreignID) |
|
137
|
|
|
|| (is_array($foreignID) && in_array($item->$foreignKey, $foreignID)) |
|
138
|
|
|
|| $foreignID == $item->$foreignKey |
|
139
|
|
|
) { |
|
140
|
|
|
$item->$foreignKey = null; |
|
141
|
|
|
$item->write(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ($this->removeCallbacks) { |
|
145
|
|
|
$this->removeCallbacks->call($this, [$item->ID]); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|