-
Notifications
You must be signed in to change notification settings - Fork 1
/
DynamicModel.php
126 lines (111 loc) · 3.72 KB
/
DynamicModel.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
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace sammaye\mongodb;
use sammaye\mongodb\validators\Validator;
use yii\db\ActiveQueryInterface;
use yii\base\DynamicModel as BaseDynamicModel;
class DynamicModel extends BaseDynamicModel
{
private $_formName;
private $_relations = [];
private $_related = [];
/**
* @inheritdoc
*/
public function __get($name)
{
if (isset($this->_related[$name]) || array_key_exists($name, $this->_related)) {
return $this->_related[$name];
}
if (isset($this->_relations[$name]) && ($value = $this->getRelation($name, false))) {
$this->_related[$name] = $value->findFor($name, $this);
return $this->_related[$name];
}
return parent::__get($name);
}
public function formName()
{
if ($this->_formName) {
return $this->_formName;
}
return parent::formName();
}
public function setFormName($name)
{
$this->_formName = $name;
return $this;
}
public function createValidators()
{
$validators = new \ArrayObject;
foreach ($this->rules() as $rule) {
if ($rule instanceof Validator) {
$validators->append($rule);
} elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type
$validator = Validator::createValidator($rule[1], $this, (array)$rule[0], array_slice($rule, 2));
$validators->append($validator);
} else {
throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
}
}
return $validators;
}
public function addRule($attributes, $validator, $options = [])
{
$validators = $this->getValidators();
$validators->append(Validator::createValidator($validator, $this, (array) $attributes, $options));
return $this;
}
public function hasOne($class, $link)
{
/* @var $class ActiveRecordInterface */
/* @var $query ActiveQuery */
$query = $class::find();
$query->primaryModel = $this;
$query->link = $link;
$query->multiple = false;
return $query;
}
public function hasMany($class, $link)
{
/* @var $class ActiveRecordInterface */
/* @var $query ActiveQuery */
$query = $class::find();
$query->primaryModel = $this;
$query->link = $link;
$query->multiple = true;
return $query;
}
public function addRelation($name, $type, $class, $link)
{
$this->_relations[$name] = [$type, $class, $link];
return $this;
}
public function getRelation($name, $throwException = true)
{
// the relation could be defined in a behavior
list($type, $class, $link) = $this->_relations[$name];
$relation = $this->$type($class, $link);
if (!$relation instanceof ActiveQueryInterface) {
if ($throwException) {
throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".');
} else {
return null;
}
}
return $relation;
}
public function populateRelation($name, $records)
{
$this->_related[$name] = $records;
return $this;
}
public function isRelationPopulated($name)
{
return array_key_exists($name, $this->_related);
}
}