<?php
/** 
*   @author: Manuel Montoya 
*   Chipotle SoftwareTM 2002-2008  
*   GPLv3  
**/
class AppModel extends Model{
 
 public $assocs = array(
        'Book' => array(
            'type' => 'belongsTo',
            'className' => 'Book',
            'foreignKey' => 'collection_id',
        ),
        'Story' => array(
            'type' => 'hasOne',
            'className' => 'Story',
        ),
        'Album' => array(
            'type' => 'belongsTo',
            'className' => 'Album',
            'foreignKey' => 'collection_id',
        ),
        'Photo' => array(
            'type' => 'hasOne',
            'className' => 'Photo',
        ),
        'Post' => array(
            'type' => 'hasMany',
            'className' => 'Post',
            'order' => 'Post.id DESC',
        ),
    ); 
 
 public function expects($array) 
 {
     foreach ($array as $assoc) 
     {
            $this->bindModel
	    (
                array($this->assocs[$assoc]['type'] =>
			array($assoc => $this->assocs[$assoc]))
            );
      }
 }

/** 
*   Unbinds all unneeded associations  
*   @author: Oth aka KrazyLegs
*   @reference: http://othy.wordpress.com/2006/06/03/unbind-all-associations-except-some/ 
**/

public function unbindAll($params = array())
{
  foreach($this->__associations as $ass) // this array returns hasOne, HasMany, etc   
  {
     if (!empty($this->{$ass}))  // if hasMany assos exist, if HasOne exist, etce
     {
        $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;
 }
  
 public function invalidate($field, $value = true) 
 {
   return parent::invalidate($field, __($value, true));
 }
}
?>
