CREATE TABLE posts ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(50), body TEXT, created DATETIME DEFAULT NULL, updated DATETIME DEFAULT NULL );
INSERT INTO posts (title,body,created) VALUES ('The title', 'This is the post body.', NOW()); INSERT INTO posts (title,body,created) VALUES ('A title once again', 'And the post body follows.', NOW()); INSERT INTO posts (title,body,created) VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());
ID Title Created 1 The title 2005-07-03 10:52:21 2 A title once again 2005-07-03 10:52:34 3 Title strikes back 2005-07-03 10:52:43
为什么我没看到呢?? 如果你遇到了一个页面,上面说“not Found: The requested URL /posts/index was not found on this server,”你可能要使用 http://localhost/cake/index.php?url=posts 来访问 . 很明显,这很不美观。如果你遇到了一个页面上说“Fatal error: Call to a member function on a non-object ...”那么你可能要检查一下你的配置内容,有没有把config/database.php.default 改名为config/database.php. 参见 Blog指南中的故障及解决方法。
You are seeing this error because the action is not defined in controller Posts Notice: this error is being rendered by the app/views/errors/missing_action.thtml view file, a user-customizable error page for handling invalid action dispatches. Error: Unable to execute action on Posts
噢~,对了,我们忘记添加PostsController::view()行为了。让我们现在完成它:
app/controllers/posts_controller.php
class PostsController extends PostsHelper { function index() { }
function view($id) { $this->models['post']->setId($id); $this->set('data', $this->models['post']->read()); } }
class PostsController extends PostsHelper { function index() { }
function view($id) { $this->models['post']->setId($id); $this->set('data', $this->models['post']->read()); }
function add() { if (empty($this->params['data'])) { $this->render(); } else { if ($this->models['post']->save($this->params['data'])) { $this->flash('Your post has been saved.','/posts'); } else { $this->set('data', $this->params['data']); $this->validateErrors($this->models['post']); $this->render(); } } } }
?>
同时行为的模版文件是:
app/views/posts/add.thtml
Add post to blog
formTag('/posts/add')?>
Title: inputTag('post/title', 40)?> tagErrorMsg('post/title', 'Title is required.') ?>
Body: areaTag('body') ?> tagErrorMsg('post/body', 'Body is required.') ?>
submitTag('Save') ?>
现在你可以通过地址"/cake/posts/add"来访问 add 页面page at the address "/cake/posts/add", 或者,我们可以在索引页面的底部放上一个快捷的“Add new post”连接:
class Post extends AppModel { var $validate = array( 'title'=>VALID_NOT_EMPTY, 'body'=>VALID_NOT_EMPTY); }
?>
了解API文档中更多关于有效检验器的内容。
删除一个帖子 app/controllers/posts_controller.php
class PostsController extends PostsHelper { function index() { }
function view($id) { $this->models['post']->setId($id); $this->set('data', $this->models['post']->read()); }
function add() { if (empty($this->params['data'])) { $this->render(); } else { if ($this->models['post']->save($this->params['data'])) { $this->flash('Your post has been saved.','/posts'); } else { $this->set('data', $this->params['data']); $this->validateErrors($this->models['post']); $this->render(); } } }
function delete($id) { if ($this->models['post']->del($id)) { $this->flash('The post with id: '.$id.' has been deleted.', '/posts'); } } }
linkTo($post['title'], "/posts/view/{$post['id']}")?> linkTo('Delete',"/posts/delete/{$post['id']}", null, "Are you sure you want to delete post entitled \'{$post['title']}\'?")?>
linkTo('Add new post', '/posts/add') ?>
在完成它之后,我们就可以删除那些空白标题的帖子了。
编辑帖子 app/controllers/posts_controller.php
class PostsController extends PostsHelper { function index() { }
function view($id) { $this->models['post']->setId($id); $this->set('data', $this->models['post']->read()); }
function add() { if (empty($this->params['data'])) { $this->render(); } else { if ($this->models['post']->save($this->params['data'])) { $this->flash('Your post has been saved.','/posts'); } else { $this->set('data', $this->params['data']); $this->validateErrors($this->models['post']); $this->render(); } } }
function delete($id) { if ($this->models['post']->del($id)) { $this->flash('The post with id: '.$id.' has been deleted.', '/posts'); } }
function edit($id=null) { if (empty($this->params['data'])) { $this->models['post']->setId($id); $this->params['data']= $this->models['post']->read(); $this->render(); } else { $this->models['post']->set($this->params['data']); if ( $this->models['post']->save()) { $this->flash('Your post has been updated.','/posts'); } else { $this->set('data', $this->params['data']); $this->validateErrors($this->models['post']); $this->render(); } } } }
?>
app/views/posts/edit.thtml
Edit post to blog
formTag('/posts/edit')?>
Title: inputTag('post/title', 40)?> tagErrorMsg('post/title', 'Title is required.') ?>
areaTag('body') ?> tagErrorMsg('post/body', 'Body is required.') ?>
submitTag('Save') ?>
你也可以在表单标签中用
hiddenTag('id')?>
来代替直接使用html的标签。
同时, 在 index.thtml 中, 我们添加一个编辑连接:
Blog posts
Id
Title
Created
post->findAll() as $post): ?>
linkTo($post['title'], "/posts/view/{$post['id']}")?> linkTo('Delete',"/posts/delete/{$post['id']}", null, "Are you sure you want to delete post entitled \'{$post['title']}\'?")?> linkTo('Edit',"/posts/edit/{$post['id']}")?>