[go: up one dir, main page]

Menu

[r5]: / models / app_model.php  Maximize  Restore  History

Download this file

211 lines (181 with data), 4.3 kB

  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
<?php class AppModel extends Model
{
function afterFind($results)
{
if (isset($this->__runResetExpects) && $this->__runResetExpects)
{
$this->__resetExpects();
unset($this->__runResetExpects);
}
return parent::afterFind($results);
}
/**
* Unbinds all relations from a model except the specified ones. Calling this function without
* parameters unbinds all related models.
*
* @access public
* @since 1.0
*/
function expects()
{
$models = array();
$arguments = func_get_args();
$innerCall = false;
if (!empty($arguments) && is_bool($arguments[0]))
{
$innerCall = $arguments[0];
}
foreach($arguments as $index => $argument)
{
if (is_array($argument))
{
if (count($argument) > 0)
{
$arguments = am($arguments, $argument);
}
unset($arguments[$index]);
}
}
foreach($arguments as $index => $argument)
{
if (!is_string($argument))
{
unset($arguments[$index]);
}
}
if (count($arguments) == 0)
{
$models[$this->name] = array();
}
else
{
foreach($arguments as $argument)
{
if (strpos($argument, '.') !== false)
{
$model = substr($argument, 0, strpos($argument, '.'));
$child = substr($argument, strpos($argument, '.') + 1);
if ($child == $model)
{
$models[$model] = array();
}
else
{
$models[$model][] = $child;
}
}
else
{
$models[$this->name][] = $argument;
}
}
}
$relationTypes = array ('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
foreach($models as $bindingName => $children)
{
$model = null;
foreach($relationTypes as $relationType)
{
$currentRelation = (isset($this->$relationType) ? $this->$relationType : null);
if (isset($currentRelation) && isset($currentRelation[$bindingName]) && is_array($currentRelation[$bindingName]) && isset($currentRelation[$bindingName]['className']))
{
$model = $currentRelation[$bindingName]['className'];
break;
}
}
if (!isset($model))
{
$model = $bindingName;
}
if (isset($model) && $model != $this->name && isset($this->$model))
{
if (!isset($this->__backInnerAssociation))
{
$this->__backInnerAssociation = array();
}
$this->__backInnerAssociation[] = $model;
$this->$model->expects(true, $children);
}
}
if (isset($models[$this->name]))
{
foreach($models as $model => $children)
{
if ($model != $this->name)
{
$models[$this->name][] = $model;
}
}
$models = array_unique($models[$this->name]);
$unbind = array();
foreach($relationTypes as $relation)
{
if (isset($this->$relation))
{
foreach($this->$relation as $bindingName => $bindingData)
{
if (!in_array($bindingName, $models))
{
$unbind[$relation][] = $bindingName;
}
}
}
}
if (count($unbind) > 0)
{
$this->unbindModel($unbind);
}
}
if (!$innerCall)
{
$this->__runResetExpects = true;
}
}
/**
* Resets all relations and inner model relations after calling expects() and find().
*
* @access private
* @since 1.1
*/
function __resetExpects()
{
if (isset($this->__backAssociation))
{
$this->__resetAssociations();
}
if (isset($this->__backInnerAssociation))
{
foreach($this->__backInnerAssociation as $model)
{
$this->$model->__resetExpects();
}
unset($this->__backInnerAssociation);
}
}
function unbindAll($params = array())
{
foreach($this->__associations as $ass)
{
if(!empty($this->{$ass}))
{
$this->__backAssociation[$ass] = $this->{$ass};
if(isset($params[$ass]))
{
foreach($this->{$ass} as $model => $detail)
{
if(!in_array($model,$params[$ass]))
{
$this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
unset($this->{$ass}[$model]);
}
}
}else
{
$this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
$this->{$ass} = array();
}
}
}
return true;
}
}?>