unbind All associations except some
This is a very handy method ( in your AppModel ).
Many times you have a model that has a lot of associations, and for some stuff, you don’t want the overhead of all the associated data. For that you use $this->recursive = ‘-1’, this turns all associations completly. or you can use calls to unbindModel. but then sometimes, you don’t know what associations the model has ( ie. a piece of code you wrote that is used by other people, a component most likely ).
for that there is unbindAll, this method will shut off all associations except the ones passed in params, usage:
$this->Special->Product->unbindAll(array('belongsTo'=>array('Category','Manufacturer'),'hasMany'=>array('Image')));
Neat eh ? here is the code:
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;
}
.
on September 25, 2006 on 2:32 pm
Neat idea. I like it very much 🙂
on December 18, 2006 on 11:17 am
Something is wrong, or I’am wrong. I try to implement the auth component from http://bakery.cakephp.org/articles/view/148 I use simple User model.
class User extends AppModel
{
var $name = ‘User’;
}
Maybe my User model is bad?
Regards
on December 18, 2006 on 1:17 pm
class User extends AppModel
{
var $name = ‘User’;
var $belongsTo = ‘Group’;
}
user belongs To group
but this is not related to unbindAll, is it ? 😉
on December 28, 2006 on 2:27 pm
Can anyone explain why we need this code
$this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
to be called for every model in the association array? It seems is not changed for every models in the loop and just makes same(redundant) data. Same present in native cake unbindModel.
I mean why we need that besides just this: $this->__backAssociation[$ass] = $this->{$ass};
on May 13, 2007 on 4:50 pm
[…] a little teaser in form of a behavior for Cake 1.2 called ‘Containable’. Essentially it is yet another way to unbind associations from a model recursively on the fly. However, I think it’s more powerful […]
on May 14, 2007 on 4:31 pm
[…] teaser in form of a behavior for Cake 1.2 called ‘Containable’. Essentially it is yet another way to unbind associations from a model recursively on the fly. However, I think it’s more […]
on October 5, 2007 on 9:16 am
@acorus:
The __backAssociation array is used when restoring the default associations after a findAll for example. But yeah, you have a point. Maybe it that in certain circumstances, there are associations in the _backAssociation that are not in __association. In that case, your faster approach would potentially delete associations…I think.
Mind that, I don’t think that the __backAssociation should be accessed since it is private. You probably should collect an array and then call unbindModel instead.
on January 15, 2008 on 12:28 pm
[…] sy menemukan blognya Othy yang membuat sebuah fungsi yang cerdas yaitu fungsi unbindAll. Dengan fungsi unbindAll ini kita […]
on February 5, 2008 on 11:35 am
[…] so what about unbinding associated models ? … well … what I do is that I use the cool unbindAll function by Othman Ouihibi. I’ve been using it since CakePHP 1.1 days. But there’s a […]
on February 6, 2009 on 12:38 pm
telefutura futbol 21630 khz bernie mack comedy lazy town action figures the dispatch co.
on April 10, 2009 on 3:07 am
Buenas ! seria mucha molestia brindar un poco mas de info sobre este tema ??? esta muy bueno el blog, un saludo y arriba el futbol !
on October 4, 2011 on 9:41 am
okpoip
on August 13, 2012 on 9:55 am
$this->{$tableAlias}->unbindModel(array(
‘hasMany’=>array_keys($this->{$tableAlias}->hasMany),
‘belongsTo’=>array_keys($this->{$tableAlias}->belongsTo)
));
on June 19, 2013 on 10:57 pm
Hello very nice site!! Man .. Excellent .. Wonderful .
. I’ll bookmark your site and take the feeds additionally? I’m satisfied to find numerous helpful information right here
in the post, we want develop extra strategies on this
regard, thank you for sharing. . . . . .
on August 9, 2014 on 5:30 pm
Ha have a simple snippet: