This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
forked from tyohan/MongoRecord
/
EEmbeddedArraysBehavior.php
122 lines (109 loc) · 2.58 KB
/
EEmbeddedArraysBehavior.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
<?php
/**
* EEmbeddedArraysBehavior.php
*
* PHP version 5.2+
*
* @author Dariusz Górecki <darek.krk@gmail.com>
* @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
* @copyright 2011 CleverIT http://www.cleverit.com.pl
* @license http://www.yiiframework.com/license/ BSD license
* @version 1.3
* @category ext
* @package ext.YiiMongoDbSuite
* @since v1.0
*/
/**
* @since v1.0
*/
class EEmbeddedArraysBehavior extends EMongoDocumentBehavior
{
/**
* Name of property witch holds array od documents
*
* @var string $arrayPropertyName
* @since v1.0
*/
public $arrayPropertyName;
/**
* Class name of doc in array
*
* @var string $arrayDocClassName
* @since v1.0
*/
public $arrayDocClassName;
private $_cache;
/**
* @since v1.0
* @see CBehavior::attach()
*/
public function attach($owner)
{
parent::attach($owner);
// Test if we have correct embding class
if(!is_subclass_of($this->arrayDocClassName, 'EMongoEmbeddedDocument'))
throw new CException(Yii::t('yii', get_class($testObj).' is not a child class of EMongoEmbeddedDocument!'));
$this->parseExistingArray();
}
/**
* Event: initialize array of embded documents
* @since v1.0
*/
public function afterEmbeddedDocsInit($event)
{
$this->parseExistingArray();
}
/**
* @since v1.0
*/
private function parseExistingArray()
{
if(is_array($this->getOwner()->{$this->arrayPropertyName}))
{
$arrayOfDocs = array();
foreach($this->getOwner()->{$this->arrayPropertyName} as $doc)
{
$obj = new $this->arrayDocClassName;
$obj->setAttributes($doc, false);
$arrayOfDocs[] = $obj;
}
$this->getOwner()->{$this->arrayPropertyName} = $arrayOfDocs;
}
}
/**
* @since v1.0.2
*/
public function afterValidate($event)
{
parent::afterValidate($event);
foreach($this->getOwner()->{$this->arrayPropertyName} as $doc)
{
if(!$doc->validate())
$this->getOwner()->addErrors($doc->getErrors());
}
}
public function beforeToArray($event)
{
if(is_array($this->getOwner()->{$this->arrayPropertyName}))
{
$arrayOfDocs = array();
$this->_cache = $this->getOwner()->{$this->arrayPropertyName};
foreach($this->_cache as $doc)
{
$arrayOfDocs[] = $doc->toArray();
}
$this->getOwner()->{$this->arrayPropertyName} = $arrayOfDocs;
return true;
}
else
return false;
}
/**
* Event: re-initialize array of embedded documents which where toArray()ized by beforeSave()
*/
public function afterToArray($event)
{
$this->getOwner()->{$this->arrayPropertyName} = $this->_cache;
$this->_cache = null;
}
}