Update05.01.2008 At the moment the counterCache code is only executed on CREATE, not on UPDATE – a patch is under way
Hello,
Just a little turtorial to demonstrate the new model counterCache feature.
CounterCache is a very fancy little addition to our beloved CakePHP framework.Simply put, all it does is make sure that your belongsTo associations always have an updated field with the count of elements belonging to the parent.
Lets use this simple example, just two models and controllers:
Posts (posts_controller) (hasMany PostComment)
PostComments (post_comments_controller) (belongsTo Post)
SQL
-- our posts
CREATE TABLE `posts` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`post_comment_count` int(11) UNSIGNED DEFAULT NULL,
`title` varchar(100) NOT NULL,
`body` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
-- comments to our post
CREATE TABLE `post_comments` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`post_id` int(11) UNSIGNED NOT NULL,
`is_active` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
`name` varchar(100) NOT NULL,
`body` text NOT NULL,
PRIMARY KEY (`id`),
KEY `post_id` (`post_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
To keep things simple, we are just going to use scaffolding in controllers.
source : http://www.cakephp.nu/countercache-cakephp-12-beta
Đăng ký:
Đăng Nhận xét (Atom)
Không có nhận xét nào:
Đăng nhận xét