2012-04-17 65 views
1

我面臨着一個奇怪的問題,同時創造CakePHP中2.1CakePHP的模型錯誤

錯誤編輯功能genreated: 非法偏移類型[CORE \蛋糕\型號\ Model.php,行2689]

我的編輯.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'; 
    } 
    ?> 

控制器: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.'); 
       } 
      } 
     } 
    } 

?> 
+1

您可以發佈您的相關型號和控制器代碼嗎? 'edit.ctp'本身並不能真正幫助確定錯誤。 – mensch 2012-04-17 08:58:56

+0

和你的控制器代碼?最好用代碼編輯你的問題,這樣你就可以正確格式化它,而不是將它作爲評論發佈。 – mensch 2012-04-17 09:04:00

回答

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

+0

錯誤已解決,但我的問題是此代碼不預先填充文本框中的ID爲1的數據。 – 2012-04-17 09:25:48

+0

@RohitKumarChoudhary您是否檢查過「$ id」確實是一個?你有沒有檢查你的數據是否確實包含ID爲1的行?做那些基本檢查 – 2012-04-17 09:27:50

+0

是的,我確實已經檢查過這個。我的桌子包含這個。 – 2012-04-17 09:40:40

-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 /> 
+0

這不是事實。如果你的控制器和模型設置正確,CakePHP會爲你做。 – 2013-07-31 09:02:49