Posted on 13/5/07 by Felix Geisendörfer
The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.
Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.
Update: JCSiegrist just noticed a problem in the code as I've released it. Somehow I must have deleted an important line of code right before uploading, sry about this. Please get the new version of the code.
for those of you who feel like it's becoming a regular thing that I'm starting my posts with excuses for not having posted stuff in a while there is good news: I'm one final Math / History exam away from being a free man and done with school. That means I'll start heavy-duty blogging again, publishing all the good and great stuff I've been working on and learning in the past months.
For those of you who can't await this, here comes 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 then all the ones released so far for several reasons:
- It's a behavior! No manual AppModel hacking here ; ).
- It is smart: It will recognize when you pass in conflicting associations and automatically correct that.
- It's for the lazy: Always wondering what $recursive parameter to use? No More! Containable sets it for you.
- It's convenient: Like to use dot-seperated Model strings as parameters? Arrays of Models? Or both at the same time? No problem!
Alright, enough marketing. Here is the code and how to install it:
- Put the code in /app/models/behaviors/containable.php
- Create an /app/app_model.php file like this:
var $actsAs = array('Containable');
Done! Now that was easy. But how to use this new behavior? Well, how would you like to use it? See if one of those alternatives cuts it for you:
'Group' => array(
'Post' => array(
'Comment' => 'User',
Uhm, that was verbose, how about:
Or a slightly longer, but more DRY alternative:
'Post' => array('Category', 'RelatedPost', 'Comment.User')
All of the snippets above will cause the exactly the same associations to be present in your next Model::find* query. They are just expressed in a different notation. The function does not care if you use several parameters, strings with dot notation or nested arrays. Define your own containment policies in whatever syntax you like the best! Or mix them : )!
I hope this will become a powerful weapon in your very own war on terrorizing associations you don't want in your resultsets and will make your present doomsday devices obsolete soon ; ).
Oh and before I forget, it might also be worth to read through the code a little. I've experimented with two cool things in there: Namely using static variables as a 'memory' in recursive functions as well as recognizing the root level of the recursive calls by checking if a certain parameter is a boolean : ).
-- Felix Geisendörfer aka the_undefined
PS: Let me know if you find any bugs so I can fix them ASAP.
You can skip to the end and add a comment.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.