| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @author: Manuel Montoya |
|---|
| 4 | * Chipotle SoftwareTM 2002-2008 |
|---|
| 5 | * GPLv3 |
|---|
| 6 | **/ |
|---|
| 7 | class AppModel extends Model{ |
|---|
| 8 | |
|---|
| 9 | public $assocs = array( |
|---|
| 10 | 'Book' => array( |
|---|
| 11 | 'type' => 'belongsTo', |
|---|
| 12 | 'className' => 'Book', |
|---|
| 13 | 'foreignKey' => 'collection_id', |
|---|
| 14 | ), |
|---|
| 15 | 'Story' => array( |
|---|
| 16 | 'type' => 'hasOne', |
|---|
| 17 | 'className' => 'Story', |
|---|
| 18 | ), |
|---|
| 19 | 'Album' => array( |
|---|
| 20 | 'type' => 'belongsTo', |
|---|
| 21 | 'className' => 'Album', |
|---|
| 22 | 'foreignKey' => 'collection_id', |
|---|
| 23 | ), |
|---|
| 24 | 'Photo' => array( |
|---|
| 25 | 'type' => 'hasOne', |
|---|
| 26 | 'className' => 'Photo', |
|---|
| 27 | ), |
|---|
| 28 | 'Post' => array( |
|---|
| 29 | 'type' => 'hasMany', |
|---|
| 30 | 'className' => 'Post', |
|---|
| 31 | 'order' => 'Post.id DESC', |
|---|
| 32 | ), |
|---|
| 33 | ); |
|---|
| 34 | |
|---|
| 35 | public function expects($array) |
|---|
| 36 | { |
|---|
| 37 | foreach ($array as $assoc) |
|---|
| 38 | { |
|---|
| 39 | $this->bindModel |
|---|
| 40 | ( |
|---|
| 41 | array($this->assocs[$assoc]['type'] => |
|---|
| 42 | array($assoc => $this->assocs[$assoc])) |
|---|
| 43 | ); |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Unbinds all unneeded associations |
|---|
| 49 | * @author: Oth aka KrazyLegs |
|---|
| 50 | * @reference: http://othy.wordpress.com/2006/06/03/unbind-all-associations-except-some/ |
|---|
| 51 | **/ |
|---|
| 52 | |
|---|
| 53 | public function unbindAll($params = array()) |
|---|
| 54 | { |
|---|
| 55 | foreach($this->__associations as $ass) // this array returns hasOne, HasMany, etc |
|---|
| 56 | { |
|---|
| 57 | if (!empty($this->{$ass})) // if hasMany assos exist, if HasOne exist, etce |
|---|
| 58 | { |
|---|
| 59 | $this->__backAssociation[$ass] = $this->{$ass}; |
|---|
| 60 | |
|---|
| 61 | if ( isset($params[$ass]) ) |
|---|
| 62 | { |
|---|
| 63 | foreach ( $this->{$ass} as $model => $detail ) |
|---|
| 64 | { |
|---|
| 65 | if (!in_array($model,$params[$ass])) |
|---|
| 66 | { |
|---|
| 67 | $this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass}); |
|---|
| 68 | unset($this->{$ass}[$model]); |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | else |
|---|
| 73 | { |
|---|
| 74 | $this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass}); |
|---|
| 75 | $this->{$ass} = array(); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | return true; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | public function invalidate($field, $value = true) |
|---|
| 83 | { |
|---|
| 84 | return parent::invalidate($field, __($value, true)); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | ?> |
|---|