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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
A Glimpse Inside CakePHP 1.2
<?php
class Post extends AppModel {
var $hasAndBelongsToMany = array(
?Tag? => array(
?className? => ?Tag?,
?with? => ?TaggedPost?,
)
);
function beforeSave() {
if(!empty($this->data[?Tag?])) {
$this->TaggedPost->save($this->data[?Tag?]);
}
}
}
?>
<?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?];
echo $tag[?TaggedPost?][?date?];
endforeach;
?>