One of my co-workers over at CDC (the mighty gwoo) gave a talk to the Orange Country PHP group about CakePHP 1.2 and some of the features that it contains. One of the more interesting items, well interesting to *me* anyway, is the addition of a convenience feature to "has and belongs to many" associations called "with". Stolen directly from gwoo's slides, here's an example of it:
PHP:
-
<?php
-
class Post extends AppModel {
-
var $hasAndBelongsToMany =
array(
-
-
‘className’ => ’Tag’,
-
‘with’ => ‘TaggedPost’,
-
)
-
);
-
-
function beforeSave() {
-
if(!
empty($this->
data[‘Tag’
])) {
-
$this->TaggedPost->save($this->data[‘Tag’]);
-
}
-
}
-
}
-
?>
So what is the "with" parameter really for? It's nothing more than a convenience parameter that lets you apply a label to the name of your join table, so you don't have to call it by it's ugly name, in this case PostTags. Want to see it in action?
PHP:
-
<?php
-
class PostsController extends AppController {
-
var $name = ‘Posts’;
-
function tags() {
-
$this->set(‘tags’, $this->Post->TaggedPost->findAll());
-
}
-
}
-
?>
-
-
<?php
-
foreach ($tags as $tag) :
-
echo $tag[‘Post’
][‘title’
];
-
echo $tag[‘Tag’
][‘name’
];
-
-
endforeach;
-
?>
It's little touches like that, unseen by a lot of developers, that makes CakPHP just a little bit easier to use with each passing day. You can download gwoo's slides here.
Add your comment below, or
trackback from your own site.
Subscribe to these comments.
Be nice. Keep it clean. Stay on topic. No spam.
You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
hey Chris
it's actually for more than that. The join table can now have extra fields, and you can specify the model. Look at page 8 of the OC pdf.
e.g. my join table can now have extra fields like a quantity and a date, which I then build a model for and can access CRUD on. Some HABTM join tables need this kind of meta-data .
Hi Chris,
I tried to use it like the way it's explained in the sheets, however 1 or 2 days after the presentation, the trunk version changed and the 'with' statement doesn't work like that anymore with HABTM relations.
If you look in the API, you see the relation is automatically created with a HABTM relation. Only thing is you have to call things differently now.
You could read a discussion I had (dlmax) here:
http://logs.cakephp.nu/cakephp/chat.log.2007-08-19#line_14_07_marek796
Cheers,
Max
@Luke
Yeah, I saw some of that other new stuff and even to get to work with some of it because we generally eat our own dogfood at CDC, using the bleeding edge 1.2.x branch as long as it doesn't blow anything up
@Max
The "with" parameter is more of a convenience thing, allowing you to give a name to the join table if you want to use that instead of the actual table name. I know that PhpNut did a bunch of refactoring to greatly reduce the number of queries needed for some HABTM relationships as well.
Unsure why you'd want to use a join table for more than just joining as that's the purpose...beyond that I'd fear being inefficient and confusing.
However this is a SUPER handy convenience. Thanks for sharing! There's a lot of little things you don't normally see about CakePHP at first or even second glance.
Hrmm... this no longer appears to work for me for 1.2.x.x from a fresh svn download.
Only the TaggedPost items appear to be populated into the array.
Any ideas?
Thanks!
R
if you wanna use another name for the join model
you have to create the appropriate model class (a define the table and all nonstandardly named stuff) and use the model name in 'with' param in the HABTM