unbindAll
Here is a simple way to unbind all models. Just put this function into your app_model.php
Model Class:
Download code
<?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);
}
?>
Comments
Comment
1 recursive
Comment
2 before running bindModel
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.
Comment
3 Another unbindAll
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;
}
?>
Comment
4 Found The Author
The original can be downloaded from here:
http://othy.wordpress.com/2006/06/03/unbind-all-associations-except-some/