第五章 一个博客系统(blog)的完整开发示例
本帖最后由 doutu 于 2009-4-3 18:07 编辑本贴,出处:http://bsdcn.com/viewthread.php?tid=60
Kohana PHP 技术的QQ群:58987322. Kohana 是一款纯 PHP5 的框架,基于 MVC 模式开发, 它的特点就是高安全性,轻量级代码,容易使用 ...
第五章 一个博客系统(blog)的完整开发示例
5.1 数据建模
我们这里做的是一个单用户的博客系统。系统以帖子(posts)为中心,帖子具有一个类别(labels),帖子允许匿名评价(comments)。
由于是单用
户,我们的用户表(users)只会有一个管理员的帐号。
如果你的Kohana环境部署有困难,请参考:http://bsdcn.com/viewthread.php?tid=61,以下示例采用的是UTF-8编辑,如果你在输入中文时有乱码,请注意检查相关编辑设置。
下面我们根据以上需求来建立数据库ORM模型:
所有的博客帖子都由系统管理员发布,即帖子和用户是“多对一”的关系。用户和帖子的关系是“属于”关系。每个帖子可以由多个评价,每个贴子也属于一个分类,每个分类下面也会有多个贴子。
[*]<?php[*]// application/modeles/users.php[*][*]class User_Model extends ORM {[*] protected $has_many = array("posts");[*]}
复制代码
[*]<?php[*]// application/modeles/post.php[*][*]class Post_Model extends ORM {[*] protected $belongs_to = array("user", "label");[*] protected $has_many = array("comments");[*]}
复制代码
[*]<?php[*]// application/modeles/label.php[*][*]class Label_Model extends ORM {[*] protected $has_many = array("posts");[*]}
复制代码 考虑到每个贴子可以有多个评论,我们建立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=MyISAMDEFAULT 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=MyISAMDEFAULT 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=MyISAMDEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
--
-- 导出表中的数据 `users`
--
INSERT INTO `users` (`id`, `username`, `password`) VALUES
(1, 'pat', '12345678');
复制代码 建立POST控制器,并用index方法来列出博客里面的文章。每个文章都对应于一个作者和一个分类。
Kohana_v2.3.2\application\controllers\post.php<?php
class Post_Controller extends Controller {
public function index()
{
$posts = ORM::factory('post')->with('user')->with('label')->find_all();
$view = new View('index');
$view->posts = $posts;
$view->render(TRUE);
}
}
复制代码对应的视图放到:
Kohana_v2.3.2\application\view\index.php<table border="1">
<?php foreach($posts AS $p) {?>
<tr><td>
<?= $p->title ?>
</td><td>
<?= $p->label->name ?>
</td><td>
<?= $p->user->username ?>
</td></tr>
<?php } ?>
复制代码打开浏览器,测试该页面:
http://localhost/post/index
显示内容如下:
this is first title PHP pat
this is 2nd title JAVA pat 面开始增加发布贴子功能。
第一步,先制作一个post视图:<h1>添加帖子视图</h1>
文件位置:application/view/add.php
<?=form::open("post/add")?>
标题:<?=form::input('title', 'pat')?> <br />
内容:<?=form::textarea("content",'')?> <br />
分类:<?=form::dropdown('label',$labels,'standard')?> <br />
<?=form::submit('submit', '发布')?>
<?=form::close()?>
复制代码然后在post控制器中增加add方法:
文件位置:application/controller/post.php<?php
class Post_Controller extends Controller {
public function index()
{
$posts = ORM::factory('post')->with('user')->with('label')->find_all();
$view = new View('index');
$view->posts = $posts;
$view->render(TRUE);
}
public function add()
{
if ($post = $this->input->post())
{
$post = ORM::factory('post');
$post->label_id = $this->input->post('label');
$post->user_id = 1;
$post->title = $this->input->post('title');
$post->content = $this->input->post('content');
$post->save();
}
$labels = ORM::factory('label')->select_list('id', 'name');
$view = new View('add');
$view->labels = $labels;
$view->render(TRUE);
}
}
复制代码打开 http://localhost/post/add试着添加几条数据。
然后再浏览:http://localhost/post/ 看看贴子多了几条。
this is first title PHP pat
this is 2nd title JAVA pat
pat PHP pat
第四 JAVA pat 这边也顶顶 加点图效果更佳。
群号有吴,我找到对的,帮忙更正了 刚来,顶下 本帖最后由 www.99n9.com 于 2010-10-17 22:25 编辑
3版不一样了。还有我用var_dump()看不了数据构造
页:
[1]