Cakephp模型错误

时间:2012-04-17 08:54:14

标签: cakephp

我在cakephp 2.1

中创建编辑功能时遇到了一个奇怪的问题

错误处理: 非法偏移类型[CORE \ Cake \ Model \ Model.php,第2689行]

我的edit.ctp文件是

<?php echo $this->Form->create('Task');?>
<fieldset>
<legend>Edit Task</legend>

<?php
   echo $this->Form->hidden('id');
   echo $this->Form->input('title');
   echo $this->Form->input('done');
?>

</fieldset>
<?php echo $this->Form->end('Save');?>

模型:Task.php

 <?php
    class Task extends AppModel {
        var $name = 'Task';
    }
    ?>

Controller:TasksController.php

    <?php

        class TasksController extends AppController {
            var $name = 'Tasks';
            var $helpers = array('Html', 'Form');
            function index() {
                $this->set('tasks', $this->Task->find('all'));
            }
            function add() {
                if (!empty($this->data)) {

                $this->Task->create();
                if($this->Task->save($this->data)){
                    $this->Session->setFlash('The Task has been saved');
                    $this->redirect(array('action'=>'index'),null,true);
                }else{
                    $this->Session->setFlash('Task not saved.Try again.');
                }      

            }
        }
        function edit($id = null) {
            if (!$id) {
                $this->Session->setFlash('Invalid Task');
                $this->redirect(array('action' => 'index'), null, true);
            }
            if (empty($this->data)) {
                $this->data = $this->Task->find(array('id' => $id));
            } else {
                if ($this->Task->save($this->data)) {
                    $this->Session->setFlash('The Task has been saved');
                    $this->redirect(array('action' => 'index'), null, true);
                } else {
                    $this->Session->setFlash('The Task could not be saved.Please, try again.');
                }
            }
        }
    }

?>

2 个答案:

答案 0 :(得分:4)

我认为你的find()方法是错误的:

$this->data = $this->Task->find(array('id' => $id));

更改为

$this->data = $this->Task->find('all', array('conditions' => array('id' => $id)));

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

答案 1 :(得分:-1)

要预先填充表单上的数据,您需要执行以下操作:

<?php echo $this->Form->create('Task');?>
<fieldset>
<legend>Edit Task</legend>
<?php
echo $this->Form->hidden('id', array('value' => $this->data[0]['Task']['id']));
echo $this->Form->input('title', array('value' => $this->data[0]['Task']['title']));
echo $this->Form->input('done', array('value' => $this->data[0]['Task']['done']));
//var_dump($this->data[0]['Task']['id']);
?>
</fieldset>
<?php echo $this->Form->end('Save');?>
<?php echo $this->Html->link('List All Tasks', array('action'=>'index')); ?><br />
<?php echo $this->Html->link('Add Task', array('action'=>'add')); ?><br />
<?php echo $this->Html->link('List Done Tasks', array('action'=>'index')); ?><br />
<?php echo $this->Html->link('List Pending Tasks', array('action'=>'index')); ?><br />