Oth\’s Thoughts


unbind All associations except some

Posted in CakePHP by othy on June 3, 2006

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;
}

.

12 Responses to 'unbind All associations except some'

Subscribe to comments with RSS or TrackBack to 'unbind All associations except some'.


  1. Neat idea. I like it very much :)

  2. Tommm said,

    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

  3. othy said,

    class User extends AppModel
    {
    var $name = ‘User’;
    var $belongsTo = ‘Group’;
    }

    user belongs To group

    but this is not related to unbindAll, is it ? ;)

  4. acorus said,

    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};


  5. [...] 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 [...]


  6. [...] 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 [...]

  7. Tim said,

    @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.


  8. [...] sy menemukan blognya Othy yang membuat sebuah fungsi yang cerdas yaitu fungsi unbindAll. Dengan fungsi unbindAll ini kita [...]


  9. [...] 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 [...]


  10. Buenas ! seria mucha molestia brindar un poco mas de info sobre este tema ??? esta muy bueno el blog, un saludo y arriba el futbol !

  11. hgj said,

    okpoip


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.