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:

PHP:
  1. <?php
  2. class Post extends AppModel {
  3. var $hasAndBelongsToMany = array(
  4.  ‘Tag’ => array(
  5.  ‘className’ => ’Tag’,
  6.  ‘with’ => ‘TaggedPost’,
  7.  )
  8.  );
  9.  
  10.     function beforeSave() {
  11.          if(!empty($this->data[‘Tag’])) {
  12. $this->TaggedPost->save($this->data[‘Tag’]);
  13.          }
  14.     }
  15. }
  16. ?>

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:
  1. <?php
  2. class PostsController extends AppController {
  3. var $name = ‘Posts’;
  4. function tags() {
  5. $this->set(‘tags’, $this->Post->TaggedPost->findAll());
  6. }
  7. }
  8. ?>
  9.  
  10. <?php
  11. foreach ($tags as $tag) :
  12. echo $tag[‘Post’][‘title’];
  13. echo $tag[‘Tag’][‘name’];
  14. echo $tag[‘TaggedPost’][date];
  15. endforeach;
  16. ?>

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.

6 Responses to this post.

  1. Luke aka battez's Gravatar

    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 .

  2. Max's Gravatar

    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

  3. Chris Hartjes's Gravatar

    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.

  4. Tom's Gravatar

    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.

  5. Raphael Kraus's Gravatar

    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

  6. emilk's Gravatar

    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

Respond to this post

Want to advertise on this blog? Send email to chartjes@littlehart.net
GTcars Canadian Car Audio TurboDodge Car For Sale Sign
Audi Forum Mustang Forum Dodge Intrepid Miata Turbo
GTscene Pontiac Bonneville


@TheKeyboard is Digg proof thanks to caching by WP Super Cache!