Action view,add,edit,delete used in cakephp

<?php
class PostsController extends AppController{
 var $name = "Posts";
 var $components = array("Math");

 function index(){
  $res = $this->Post->find("all",array('order' => array('id DESC')));//相当于select * from post order by id desc
  $this->set("posts",$res);//将从表中查询出来的结果存于$posts以便在视图中访问
 }
 function view($id=NULL){
  $this->set("post",$this->Post->read(null,$id));//根据传入的id号$id查询表中的该条record,并存结果于$post以便在view视图中显示;
 }
 function add(){
  if(!empty($this->data))//表单中通过post传输的数据都能以$this->data(数组形式)访问
  {
   if($this->Post->save($this->data))//往表中插入数组$this->data,cakephp会辨别次序
   {
    $this->Session->setFlash("Created a Post successfully!");//显示成功时的信息
    $this->redirect(array("action"=>"index"));//成功后跳转到index下
   }
   else $this->Session->setFlash("Created a Post fail,please try again!");
  }
 }
 function edit($id=null){
  if(empty($this->data)){
   $this->data = $this->Post->read(null,$id);
  }else{
   if($this->Post->save($this->data)){
    $this->Session->setFlash("Updated Post successfully!");
    $this->redirect(array("action"=>"index"));
   }
  }
 }
 function delete($id=null){
  if($this->Post->delete($id)){
   $this->Session->setFlash("Deleted Post successfully!");
   $this->redirect(array("action"=>"index"));
  }
 }
 function test(){
  $result = $this->Math->xiangjia(19,20);
  $this->set("result",$result);
 }
}

你可能感兴趣的:(TO,in,delete,action,use,cakephp,and,how,view、add、edit)