unbindAll

by cornernote
Here is a simple way to unbind all models. Just put this function into your app_model.php

Model Class:

<?php 
  
function unbindModelAll()
  {
    
$unbind = array();
    foreach (
$this->belongsTo as $model=>$info)
    {
      
$unbind['belongsTo'][] = $model;
    }
    foreach (
$this->hasOne as $model=>$info)
    {
      
$unbind['hasOne'][] = $model;
    }
    foreach (
$this->hasMany as $model=>$info)
    {
      
$unbind['hasMany'][] = $model;
    }
    foreach (
$this->hasAndBelongsToMany as $model=>$info)
    {
      
$unbind['hasAndBelongsToMany'][] = $model;
    }
    
parent::unbindModel($unbind);
  }
?>

Report

More on Snippets

Advertising

Comments

  • enigma posted on 08/28/09 08:09:33 AM

    Model Class:

    <?php 
    class AppModel extends Model {
            function 
    unbindModelAll() {
                    foreach(array(
                            
    'hasOne' => array_keys($this->hasOne),
                            
    'hasMany' => array_keys($this->hasMany),
                            
    'belongsTo' => array_keys($this->belongsTo),
                            
    'hasAndBelongsToMany' => array_keys($this->hasAndBelongsToMany)
                    ) as 
    $relation => $model) {
                            
    $this->unbindModel(array($relation => $model));
                    }
            }
    }

    ?>
  • cornernote posted on 12/27/06 06:33:00 AM
    The snippet above was written by Othman ouahbi (aka: CraZyLeGs)

    The original can be downloaded from here:
    http://othy.wordpress.com/2006/06/03/unbind-all-associations-except-some/
  • cornernote posted on 12/27/06 06:18:56 AM
    I found another one. This one looks better. I can't remember who made it but I thought I'd post it here. If your the author please feel free to post a comment and take the credit. My main point is that it wasn't me. :)

    Model Class:

    <?php 
      
    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;
      }
    ?>
  • gwoo posted on 12/11/06 09:56:59 AM
    You should be able to do the same thing by just setting $this->ModelName->recursive = -1; or $this->recursive = -1; if you are in the model.
    • TommyO posted on 12/11/06 02:15:55 PM
      You should be able to do the same thing by just setting $this->ModelName->recursive = -1; or $this->recursive = -1; if you are in the model.
      That's true, but I think this is more for when you want to run bindModel on a 'clean slate'. Correct me if I'm wrong, but if you set recursive to -1 and then did a bindModel, you would not get the results of the Models associated with bindModel since there would be no recursion.

      Also, Bret, I would replace the last line, parent::unbindModel with $this->unbindModel. You should get the same results but through inheritance, which gives you override abilities both in AppController and all controllers that extend it.
login to post a comment.