CakePHP is our framework of choice here at Paravel. I spend a lot of time on CakeForge, The Bakery, the Cookbook, and the API looking for code and examples either A: because I sometimes don’t know what I’m doing, or B: I want to know THE best convention to for the answer I’m looking for.
After countless hours of using these mind-altering codes, I -authoritatively- present to the Top 4 Behaviors that everyone should be using in their apps… or else…
#4 Containable
This is hot off the shelf and new to CakePHP 1.2 and it makes a world of difference. When querying items in the database, Cake likes to SELECT records wtih all it’s belongsTo, hasMany, and HABTMs (“habtems”) auto-magically associated. With the Containable Behavior you can stop all that nonsense and speed up your app by having it contain the JOIN to a “short list” of approved “models”…
The Containable Behavior is like the bouncer at a fancy Hollywood club where if you’re not on the list, you get kicked to the curb and have to watch all the other “approved” models go by. Hot, hot models.
More concretely, let’s say you had a Posts table and you wanted to get only the comments in the view, while simultaneously ignoring all the Author, Tags, Categories, etc.
/app/models/post.php
[sourcecode language="php"]
class Post extends AppModel {
var $name = "Post";
var $actsAs = array(‘Containable’);
// forgive the over-simplification of these.
var $hasMany = array(‘Comments’);
var $belongsTo = array(‘Author’);
var $hasAndBelongsToMany = array(‘Tags’,'Category’);
}
[/sourcecode]
/app/controllers/posts_controller.php
[sourcecode language="php"]
function view_a_post_with_only_comments($id = null) {
if(!$id) {
$this->Session->setFlash(‘You forgot the ID number.’);
$this->redirect(‘/’);
}
$this->Post->contain(‘Comment’);
$this->set(‘posts’, $this->Post->read(null, $id));
}
[/sourcecode]
It’s that easy! now you’ll only be pulling the Post and the Comments! ka-chow! This
#3 Tree
I love trees. My wife has a particular affinity towards them. If you like trees then you will be a fan of this behavior. If you do not like trees, I suggest you eat a bowl of hair because you are a du-mmy. Over the course of the last year I’ve made a few category systems in my apps and all of them left me feeling like i did something wrong and/or patched the code together in a terrible way.
Enter Tree Behavior! It’s been around forever and a half, and I don’t know why I didn’t use it, but it makes organizing things like categories, forum posts, comment threads, etc a whiz! All you need is to add another couple of fields to the database and then add one line to your model.
categories.sql
[sourcecode language="sql"]
create table categories (
id integer 11 auto_increment,
name varchar 50 null,
parent_id integer 11 null,
rght integer 11 null,
lft integer 11 null
); engine=MySAM encode=UTF-8
[/sourcecode]
app/models/category.php
[sourcecode language="php"]
class Category extends Model {
var $name = ‘Category’;
var $actsAs = array(‘Tree’);
// ideally categories should describe something,
// so this is just an example association.
var $hasMany = array(‘Post’);
…
} ?>
[/sourcecode]
Now you’re cooking with grease! It will automatically keep track of your tree structure – who comes before this, who comes after (“left and right”), who is the parent, etcetera. Couldn’t be easier.
Move on to the next page for the Top 2 CakePHP behaviors:
Pages: 1 2
If you like trees – you might want to look into what are called “closure tables” and “naive trees”. They allow you to grab all the children in any branch of a tree – in one query. Makes loading trees absurdly fast.
First off, this is a very poor list. If you are critiquing excellent behaviors for this framework – they should be community ones, not packaged ones (Tree/Containable).
Secondly, MeioUpload is a very weak behavior. I question your knowledge to include this one.
Finally, why did you include sluggable? Use the Inflector class.
Hi Bary,
This post was written in 2008, before Cake 1.2 wasn’t even finalized yet. So it’s quite out of date. You should write a post on your own blog about the best CakePHP behaviors that are available. I’d be curious what’s changed for the better in the last 3+ years or so.