拿一个对于posts的数据库的举例好了: php
主页以下: 数据库
<?php
echo 'this is index:';
foreach($posts as $post){
echo "id:".$post['Post']['id']."|";
/*点击title的时候会传递出post的id*/
echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view',$post['Post']['id']))."||";
/*点击Edit的时候会传送id*/
echo $this->Html->link("Edit",
array('controller' => 'posts', 'action' => 'goto_edit',$post['Post']['id']))."||";
/*点击delete的时候会传送该Post的id*/
echo $this->Html->link('delete',
array('controller' => 'posts', 'action' => 'delete', $post['Post']['id']));
echo "<br>";
}
?>
/*建立一个新的Post,经试验代表form的名字貌似是随意取的,关键是里面的input的数据的名字,起的和数据库里的字段同样便可*/
<form name="Post" action="/lab/posts/add/" method='post'>
<input type="text" name="title"/>
<input type="submit" value="submit"/>
</form> post
////////////////////////////////////////////////////////////////////////////////////////////////////////////// this
接下来是controller: spa
<?php
class PostsController extends AppController{
/*总入口的方法*/
public function index(){
$result = $this->Post->find("all");
$this->set('posts',$result);
}
/*删除的方法,把id放入到参数中,目测应该算是相似于get的方法*/
public function delete($id){
$this->Post->delete($id);
$this->render('delete');
}
/*添加一个post的方法,因为个人表中有id自增的设置,因此没有添加id*/
public function add(){
if(!empty($this->data)){
$this->Post->create();
$this->Post->save($this->data);
// if(!);
$this->render('add');
}
}
/*这个应该算是查,详细的看一篇post的信息*/
public function view($id){
$this->set('id',$id);
$result = $this->Post->findById($id);
$this->set('title',$result['Post']['title']);
//print_r($result);
}
/*这个我设置为编辑的缓冲,点击编辑后,进入到编辑页面(中间页面),此时须要传递一个id便可*/
public function goto_edit($id){
$this->set('id',$id);
$this->render("goto_edit");
}
/*编辑的方法,不过貌似怎么和建立是同样的*/
public function edit(){
echo "this is id:".$this->data['id'];
if(empty($this->data)){
$this->data = $this->Post->read(null,$id);
}else{
$this->Post->create();
$this->Post->save($this->data);
}
$this->render("edit");
}
}
?> orm
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// rem
个人数据库是这样的,总共就两个字段,没什么特别的: get
+-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | title | varchar(50) | YES | | NULL | | | id | int(11) | NO | PRI | NULL | auto_increment | +-------+-------------+------+-----+---------+----------------+