拿一個對於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 | +-------+-------------+------+-----+---------+----------------+