关于Model CakePHP

时间:2015-02-14 13:25:48

标签: cakephp cakephp-appmodel

我在数据库中有2个表。有类别和帖子。

类别具有id,name

帖子有id,类别

我有2个文件。

  1. PostsController.php

  2. edit.ctp

  3. 我在保存

    时按edit.ctp编辑类别

    图片>> http://s704.photobucket.com/albums/ww41/018115496/edit.png

    这不是保存类别的名称,而是将类别的ID保存到帖子中。

    图片>> http://i704.photobucket.com/albums/ww41/018115496/index.png

    这是我的代码

    PostsController.php

    class PostsController extends AppController {
                public $helpers = array('Html', 'Form');
                public $uses = array('Post','Category');
      public function index() {
                             $this->set('posts', $this->Post->find('all'));
                    }
    
    
     public function edit($id = null) {
                        $this->loadModel('Category');
                        $categories = $this->Category->find("list",array('field'=>array('Category.name')));
                        $this->set("categories", $categories);
                        if (!$id) {
                            throw new NotFoundException(__('Invalid post'));
                        }
                        $post = $this->Post->findById($id);
                        if (!$post) {
                            throw new NotFoundException(__('Invalid post'));
                        }
                        if ($this->request->is(array('post', 'put'))) {
                            $this->Post->id = $id;
                            if ($this->Post->save($this->request->data)) {
                                $this->Session->setFlash(__('Your post has been updated.'));
                                return $this->redirect(array('action' => 'index'));
                            }
                            $this->Session->setFlash(__('Unable to update your post.'));
                        }
                        if (!$this->request->data) {
                            $this->request->data = $post;
                        }
                }
        } 
    

    和edit.ctp

    <?php
    echo $this->Form->create('Post');
    echo $this->Form->input('category', array('options' => $categories));
    echo $this->Form->end('Save Post');
    ?> 
    

    我想我是因为找(“列表”)但我不知道如何解决。 谢谢。

1 个答案:

答案 0 :(得分:0)

我已修复您的问题,您需要更换此

//PostsController.php
public function edit($id = null) {
        $this->loadModel('Category');
        $categories = $this->Category->find("list",array('field'=>array('Category.name','Category.name')));
        $this->set("categories", $categories);
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }
        $post = $this->Post->findById($id);
        if (!$post) {
            throw new NotFoundException(__('Invalid post'));
        }
        if ($this->request->is(array('post', 'put'))) {
            $this->Post->id = $id;
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash(__('Your post has been updated.'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to update your post.'));
        }
        if (!$this->request->data) {
            $this->request->data = $post;
        }
}

调用find('list')时,传递的字段用于确定应该用作数组键和值的内容,以及可选择将结果分组的内容。默认情况下,模型的主键用于键,显示字段(可使用模型属性displayField配置)用于值。一些进一步的例子澄清:

$justCategories = $this->Category->find('list', array(
    'fields' => array('Category.name')
));
$Categories = $this->Category->find('list', array(
    'fields' => array('Category.name', 'Category.name')
));

使用上面的代码示例,生成的变量看起来像这样:

    $justCategories = Array
(
    //[id] => 'name',
    [1] => 'Books',
    [2] => 'Baby',
    [3] => 'Business & Industrial'
)

$Categories = Array
(
    //[name] => 'name',
    ['Books'] => 'Books',
    ['Baby'] => 'Baby',
    ['Business & Industrial'] => 'Business & Industrial'
)

阅读find(‘list’) in CakePHP