cakephp对数据库的增删改查

拿一个对于posts的数据库的举例好了:

主页如下:

<?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>

//

  接下来是controller:

<?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");
        }
    }
?>

/

我的数据库是这样的,总共就两个字段,没什么特别的:

+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| title | varchar(50) | YES  |     | NULL    |                |
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
+-------+-------------+------+-----+---------+----------------+

 

转载于:https://my.oschina.net/MrHou/blog/156454

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值