|
楼主 |
发表于 2009-4-1 16:23:23
|
显示全部楼层
考虑到每个贴子可以有多个评论,我们建立Comment模型:<?php
// application/modeles/comment.php
class Comment_Model extends ORM {
protected $belongs_to = array("post");
}
复制代码为了方便大家测试,我们再把数据库建立起来。--
-- 表的结构 `comments`
--
CREATE TABLE `comments` (
`id` int(10) unsigned NOT NULL auto_increment,
`post_id` int(10) unsigned NOT NULL,
`username` varchar(16) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
-- 导出表中的数据 `comments`
--
-- --------------------------------------------------------
--
-- 表的结构 `labels`
--
CREATE TABLE `labels` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(16) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- 导出表中的数据 `labels`
--
INSERT INTO `labels` (`id`, `name`) VALUES
(1, 'PHP'),
(2, 'JAVA'),
(3, 'RAILS'),
(4, 'ASP');
-- --------------------------------------------------------
--
-- 表的结构 `posts`
--
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL auto_increment,
`user_id` int(10) unsigned NOT NULL,
`label_id` int(10) unsigned NOT NULL,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- 导出表中的数据 `posts`
--
INSERT INTO `posts` (`id`, `user_id`, `label_id`, `title`, `content`) VALUES
(1, 1, 1, 'this is first title', 'this is first content'),
(2, 1, 2, 'this is 2nd title', 'this is 2nd content');
-- --------------------------------------------------------
--
-- 表的结构 `users`
--
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL auto_increment,
`username` varchar(16) NOT NULL,
`password` varchar(16) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
--
-- 导出表中的数据 `users`
--
INSERT INTO `users` (`id`, `username`, `password`) VALUES
(1, 'pat', '12345678');
复制代码 |
|