cake学习笔记

   

    其实和Yii框架差不多,直接上MVC的CRUD操作,和Yii对比学习,cake中用到的方法需要自己加载,比如在view中用到的Html Form类,其实和Yii中的Chtml差不多,

只是需要自己加载到系统。

1、列出数据

列出所有的数据的控制器

class PostsController extends AppController {
    public $helpers = array('Html', 'Form');  //加载Html Form
  
    public function index() {
        $this->set('posts', $this->Post->find('all'));  //向视图中渲所有的数据。很优雅
    }
}

 返回的数据是数组,这是是我比较喜欢的方式,Yii中使用的的对象。

列出数据的视图

<!-- File: /app/View/Posts/index.ctp --> //居然使用的CTP文件后缀名,压力巨大啊

<h1>Blog posts</h1>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Created</th>
    </tr>

    <!-- Here is where we loop through our $posts array, printing out post info -->

    <?php foreach ($posts as $post): ?>
    <tr>
        <td><?php echo $post['Post']['id']; ?></td>
        <td>
            <?php echo $this->Html->link($post['Post']['title'],  //输出一个链接,
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?> 传入的URL的数据,分开的,感觉不爽
        </td>
        <td><?php echo $post['Post']['created']; ?></td>
    </tr>
    <?php endforeach; ?>
    <?php unset($post); ?>  //不知道为什么要清楚掉变量
</table>

2、查看数据

 控制器部分

    public function view($id = null) {  //直接传过来一个参数
        if (!$id) {
            throw new NotFoundException(__('Invalid post')); //验证是否存在,不存在抛出验证
        }

        $post = $this->Post->findById($id);
        if (!$post) {
            throw new NotFoundException(__('Invalid post'));  //没有找到,不存在抛出验证
        }
        $this->set('post', $post);
    }

视图部分

<!-- File: /app/View/Posts/view.ctp -->

<h1><?php echo h($post['Post']['title']); ?></h1> //相当于Chtml::encode()自动转编码,防止xss攻击
<p><small>Created: <?php echo $post['Post']['created']; ?></small></p>

<p><?php echo h($post['Post']['body']); ?></p>

很简单,没什么好看的、

3、添加一条数据

  控制器部分

  

public function add() {
        if ($this->request->is('post')) {  //是否是post提交
            $this->Post->create(); //创建一个空模型
            if ($this->Post->save($this->request->data)) { //存储
                $this->Session->setFlash('Your post has been saved.'); //如果成功放入一个Flash
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to add your post.');
            }
        }
    }

   视图部分,其实很简单,就是一个表单。  

<!-- File: /app/View/Posts/add.ctp -->

<h1>Add Post</h1>
<?php
echo $this->Form->create('Post');  根据模型创建一个表单,
echo $this->Form->input('title'); 输出一个字段
echo $this->Form->input('body', array('rows' => '3')); 输入一个字段
echo $this->Form->end('Save Post'); 结束,输出一个submit
?>

 这个其实比Yii的更容易理解 

根据文档,简单说明下,创建表单,需要模型作为参数,输出字段,第一个参数为字段名,第二个参数为标签选项为一个shuzu。

$this->form->end("Save Post"); //生成一个submit

 这里涉及到了数据输入到系统,那就必不可少的就是数据的验证

生成一个链接

<?php echo $this->Html->link(
    'Add Post',
    array('controller' => 'posts', 'action' => 'add')
); ?>

表单验证规则  

class Post extends AppModel {
    public $validate = array(
        'title' => array(
            'rule' => 'notEmpty'
        ),
        'body' => array(
            'rule' => 'notEmpty'
        )
    );
}

这个放在模型中的。

4、更新的操作

  更新操作其实是查询和添加的结合操作。、

  算法步骤为:

  1、查询

  2、给定一个些值

  3、验证,添加数据

  如果验证失败,抛出错误,取消存入数据、

public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid post')); 检查是否存在
    }

    $post = $this->Post->findById($id);
    if (!$post) {
        throw new NotFoundException(__('Invalid post')); //检查记录是否存在
    }

    if ($this->request->is('post') || $this->request->is('put')) { 
        $this->Post->id = $id;  //$id 输入id
        if ($this->Post->save($this->request->data)) { //加入一个值
            $this->Session->setFlash('Your post has been updated.'); //发送消息给用户
            $this->redirect(array('action' => 'index'));//重定向
        } else {
            $this->Session->setFlash('Unable to update your post.');
        }
    }

    if (!$this->request->data) {
        $this->request->data = $post;
    }
}

更新的视图

<!-- File: /app/View/Posts/edit.ctp -->

<h1>Edit Post</h1>
<?php
    echo $this->Form->create('Post'); //创建一个表单,其实和前面的没有区别
    echo $this->Form->input('title');
    echo $this->Form->input('body', array('rows' => '3'));  //文本输入框
    echo $this->Form->input('id', array('type' => 'hidden')); //这里要特别注意下一点,就是id 虽然是作为隐藏表单输入的,但是还是功过GET传过去的
    echo $this->Form->end('Save Post');

5、删除操作

  删除操作没有什么可以说明的就是一点,获取ID 然后 通知用户,确定删除、

 

public function delete($id) {
    if ($this->request->is('get')) {  //判断是否为POST参数传过来的,如果使用GET传输过来的不被允许
        throw new MethodNotAllowedException();
    }

    if ($this->Post->delete($id)) {
        $this->Session->setFlash('The post with id: ' . $id . ' has been deleted.'); //发送消息
        $this->redirect(array('action' => 'index'));
    }
}

这里必须注意的是,links和linksPost是不一样的

<?php echo $this->Form->postLink(
                'Delete',  //删除
                array('action' => 'delete', $post['Post']['id']),  //向delete的方法传一个POST 的ID过去
                array('confirm' => 'Are you sure?'));  //提示
            ?>

删除没有什么视图层

原文地址:https://www.cnblogs.com/linksgo2011/p/2932563.html