forked from canni/YiiMongoDbSuite
/
EMongoEmbeddedDocument.php
225 lines (199 loc) · 4.94 KB
/
EMongoEmbeddedDocument.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
<?php
abstract class EMongoEmbeddedDocument extends CModel
{
private static $_attributes=array();
/**
* CMap of embedded documents
* @var CMap $_embedded
*/
protected $_embedded=null;
/**
* Hold down owner pointer (if any)
*
* @var EMongoEmbeddedDocument $_owner
*/
protected $_owner=null;
/**
* Constructor.
* @param string $scenario name of the scenario that this model is used in.
* See {@link CModel::scenario} on how scenario is used by models.
* @see getScenario
*/
public function __construct($scenario='insert')
{
$this->setScenario($scenario);
$this->init();
$this->attachBehaviors($this->behaviors());
$this->afterConstruct();
$this->initEmbeddedDocuments();
}
/**
* Initializes this model.
* This method is invoked in the constructor right after {@link scenario} is set.
* You may override this method to provide code that is needed to initialize the model (e.g. setting
* initial property values.)
* @since 1.0.8
*/
public function init(){}
protected function initEmbeddedDocuments()
{
if(!$this->hasEmbeddedDocuments() || !$this->beforeEmbeddedDocsInit())
return false;
$this->_embedded = new CMap;
foreach($this->embeddedDocuments() as $name=>$docClassName)
{
$doc = new $docClassName($this->getScenario());
$doc->setOwner($this);
$this->_embedded->add($name, $doc);
}
$this->afterEmbeddedDocsInit();
}
public function onBeforeEmbeddedDocsInit($event)
{
$this->raiseEvent('onBeforeEmbeddedDocsInit', $event);
}
public function onAfterEmbeddedDocsInit($event)
{
$this->raiseEvent('onAfterEmbeddedDocsInit', $event);
}
public function onBeforeToArray($event)
{
$this->raiseEvent('onBeforeToArray', $event);
}
public function onAfterToArray($event)
{
$this->raiseEvent('onAfterToArray', $event);
}
protected function beforeToArray()
{
$event = new CModelEvent($this);
$this->onBeforeToArray($event);
return $event->isValid;
}
protected function afterToArray()
{
$this->onAfterToArray(new CModelEvent($this));
}
protected function beforeEmbeddedDocsInit()
{
$event=new CModelEvent($this);
$this->onBeforeEmbeddedDocsInit($event);
return $event->isValid;
}
protected function afterEmbeddedDocsInit()
{
$this->onAfterEmbeddedDocsInit(new CModelEvent());
}
public function __get($name)
{
if($this->hasEmbeddedDocuments() && $this->_embedded->contains($name))
return $this->_embedded->itemAt($name);
else
return parent::__get($name);
}
public function __set($name, $value)
{
if($this->hasEmbeddedDocuments() && $this->_embedded->contains($name))
{
if(is_array($value))
return $this->_embedded->itemAt($name)->attributes=$value;
else if($value instanceof EMongoEmbeddedDocument)
return $this->_embedded->add($name, $value);
}
else
parent::__set($name, $value);
}
public function afterValidate()
{
if($this->hasEmbeddedDocuments())
foreach($this->_embedded as $doc)
{
if(!$doc->validate())
{
$this->addErrors($doc->getErrors());
}
}
}
public function embeddedDocuments()
{
return array();
}
public function hasEmbeddedDocuments()
{
return count($this->embeddedDocuments()) > 0;
}
/**
* Returns the list of attribute names.
* By default, this method returns all public properties of the class.
* You may override this method to change the default.
* @return array list of attribute names. Defaults to all public properties of the class.
*/
public function attributeNames()
{
$className=get_class($this);
if(!isset(self::$_attributes[$className]))
{
$class=new ReflectionClass(get_class($this));
$names=array();
foreach($class->getProperties() as $property)
{
$name=$property->getName();
if($property->isPublic() && !$property->isStatic())
$names[]=$name;
}
if($this->hasEmbeddedDocuments())
{
$names = array_merge($names, $this->_embedded->getKeys());
}
return self::$_attributes[$className]=$names;
}
else
return self::$_attributes[$className];
}
public function toArray()
{
if($this->beforeToArray())
{
$arr = array();
foreach($this as $key=>$value)
$arr[$key]=$value;
if($this->hasEmbeddedDocuments())
foreach($this->_embedded as $key=>$value)
$arr[$key]=$value->toArray();
$this->afterToArray();
return $arr;
}
}
/**
* Return owner of this document
* @return EMongoEmbeddedDocument
*/
public function getOwner()
{
if($this->_owner!==null)
return $this->_owner;
else
return null;
}
/**
* Set owner of this document
* @param EMongoEmbeddedDocument $owner
*/
public function setOwner(EMongoEmbeddedDocument $owner)
{
$this->_owner = $owner;
}
/**
* Override default seScenario method for populating to embedded records
* @see CModel::setScenario()
*/
public function setScenario($value)
{
if($this->hasEmbeddedDocuments() && $this->_embedded !== null)
{
foreach($this->_embedded as $doc)
$doc->setScenario($value);
}
parent::setScenario($value);
}
}