| 3 | | |
| 4 | | public function expects() |
| 5 | | { |
| 6 | | $models = array(); |
| 7 | | |
| 8 | | $arguments = func_get_args(); |
| 9 | | |
| 10 | | foreach($arguments as $index => $argument) |
| 11 | | { |
| 12 | | if (is_array($argument)) |
| 13 | | { |
| 14 | | if (count($argument) > 0) |
| 15 | | { |
| 16 | | $arguments = array_merge($arguments, $argument); |
| 17 | | } |
| 18 | | |
| 19 | | unset($arguments[$index]); |
| | 3 | |
| | 4 | public $assocs = array( |
| | 5 | 'Book' => array( |
| | 6 | 'type' => 'belongsTo', |
| | 7 | 'className' => 'Book', |
| | 8 | 'foreignKey' => 'collection_id', |
| | 9 | ), |
| | 10 | 'Story' => array( |
| | 11 | 'type' => 'hasOne', |
| | 12 | 'className' => 'Story', |
| | 13 | ), |
| | 14 | 'Album' => array( |
| | 15 | 'type' => 'belongsTo', |
| | 16 | 'className' => 'Album', |
| | 17 | 'foreignKey' => 'collection_id', |
| | 18 | ), |
| | 19 | 'Photo' => array( |
| | 20 | 'type' => 'hasOne', |
| | 21 | 'className' => 'Photo', |
| | 22 | ), |
| | 23 | 'Post' => array( |
| | 24 | 'type' => 'hasMany', |
| | 25 | 'className' => 'Post', |
| | 26 | 'order' => 'Post.id DESC', |
| | 27 | ), |
| | 28 | ); |
| | 29 | |
| | 30 | public function expects($array) |
| | 31 | { |
| | 32 | foreach ($array as $assoc) |
| | 33 | { |
| | 34 | $this->bindModel |
| | 35 | ( |
| | 36 | array($this->assocs[$assoc]['type'] => |
| | 37 | array($assoc => $this->assocs[$assoc]))); |
| 21 | | } |
| 22 | | |
| 23 | | if (count($arguments) == 0) |
| 24 | | { |
| 25 | | $models[$this->name] = array(); |
| 26 | | } |
| 27 | | else |
| 28 | | { |
| 29 | | foreach($arguments as $argument) |
| 30 | | { |
| 31 | | if (strpos($argument, '.') !== false) |
| 32 | | { |
| 33 | | $model = substr($argument, 0, strpos($argument, '.')); |
| 34 | | $child = substr($argument, strpos($argument, '.') + 1); |
| 35 | | |
| 36 | | if ($child == $model) |
| 37 | | { |
| 38 | | $models[$model] = array(); |
| 39 | | } |
| 40 | | else |
| 41 | | { |
| 42 | | $models[$model][] = $child; |
| 43 | | } |
| 44 | | } |
| 45 | | else |
| 46 | | { |
| 47 | | $models[$this->name][] = $argument; |
| 48 | | } |
| 49 | | } |
| 50 | | } |
| 51 | | |
| 52 | | foreach($models as $model => $children) |
| 53 | | { |
| 54 | | if ($model != $this->name && isset($this->$model)) |
| 55 | | { |
| 56 | | $this->$model->expects($children); |
| 57 | | } |
| 58 | | } |
| 59 | | |
| 60 | | if (isset($models[$this->name])) |
| 61 | | { |
| 62 | | foreach($models as $model => $children) |
| 63 | | { |
| 64 | | if ($model != $this->name) |
| 65 | | { |
| 66 | | $models[$this->name][] = $model; |
| 67 | | } |
| 68 | | } |
| 69 | | |
| 70 | | $models = array_unique($models[$this->name]); |
| 71 | | |
| 72 | | $unbind = array(); |
| 73 | | |
| 74 | | $relations = array ('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'); |
| 75 | | |
| 76 | | foreach($relations as $relation) |
| 77 | | { |
| 78 | | if (isset($this->$relation)) |
| 79 | | { |
| 80 | | foreach($this->$relation as $currentModel) |
| 81 | | { |
| 82 | | if (!in_array($currentModel['className'], $models)) |
| 83 | | { |
| 84 | | $unbind[$relation][] = $currentModel['className']; |
| 85 | | } |
| 86 | | } |
| 87 | | } |
| 88 | | } |
| 89 | | |
| 90 | | if (count($unbind) > 0) |
| 91 | | { |
| 92 | | $this->unbindModel($unbind); |
| 93 | | } |
| 94 | | } |
| 95 | | } |
| 96 | | |
| 97 | | |
| 98 | | public function unbindAll($params = array()) |
| 99 | | { |
| 100 | | foreach($this->__associations as $ass) |
| 101 | | { |
| 102 | | if(!empty($this->{$ass})) |
| 103 | | { |
| 104 | | $this->__backAssociation[$ass] = $this->{$ass}; |
| 105 | | if(isset($params[$ass])) |
| 106 | | { |
| 107 | | foreach($this->{$ass} as $model => $detail) |
| 108 | | { |
| 109 | | if(!in_array($model,$params[$ass])) |
| 110 | | { |
| 111 | | $this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass}); |
| 112 | | unset($this->{$ass}[$model]); |
| 113 | | } |
| 114 | | } |
| 115 | | } |
| 116 | | else |
| 117 | | { |
| 118 | | $this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass}); |
| 119 | | $this->{$ass} = array(); |
| 120 | | } |
| 121 | | |
| 122 | | } |
| 123 | | } |
| 124 | | return true; |
| 125 | | } |
| | 39 | } |