4 Sep
A Glimpse Inside CakePHP 1.2:
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:
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?
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.

Posted by Luke aka battez on 04.09.07 at 10:28 pm
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 .
Posted by Max on 04.09.07 at 10:28 pm
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
Posted by Chris Hartjes on 04.09.07 at 10:28 pm
@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.
Posted by Tom on 04.09.07 at 10:28 pm
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.
Posted by Raphael Kraus on 04.09.07 at 10:28 pm
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
Posted by emilk on 04.09.07 at 10:28 pm
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