2011-11-19 64 views
0

所以我試圖用CakePHP保存表單。它是一個相當簡單的形式,但由於某種原因,INSERT語句永遠不會生成。CakePHP 2.0不保存

下面是從控制器片段:

public function add($sid = null) { 
    if($this->request->is('post')) { 
     //The app enters here. debug($this->request->data) confirms the data is there. 
     if($this->Date->save($this->request->data)) { 
      //debug($this->Date->save($this->request->data)) confirms CakePHP thinks its saving the data 
      //however looking at the data, and the MySQL log, an INSERT statement is never attempted. 
      //The flash and redirect occur as expected. 
      $this->Session->setFlash('Dates Saved!'); 
      $this->redirect(array('action' => 'school', $sid)); 
     } 
    } 
    $this->set('schoolid', $sid); 
} 

這裏是我的模型:

<?php 
class Date extends AppModel { 
    public $name = 'Date'; 
} 

這裏是視圖:

<?php 
echo $this->Html->script(array(
          'dhtmlxCalendar/dhtmlxcalendar.js' 
         )); 
echo $this->Html->css(array('dhtmlxCalendar/dhtmlxcalendar.css', 'dhtmlxCalendar/skins/dhtmlxcalendar_dhx_skyblue.css')); 
?> 
<div style="position:relative;height:380px;"> 
<?php echo $this->Form->create('Dates'); ?> 
<?php echo $this->Form->input('schoolid', array('value' => $schoolid, 'type' => 'hidden')); ?> 
<?php echo $this->Form->input('grade', array('options' => array('5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => '10', '11' => '11', '12' => '12'))); ?> 
<?php echo $this->Form->input('startreg', array('label' => 'Start Registration Date')); ?> 
<?php echo $this->Form->input('endreg', array('label' => 'End Registration Date')); ?> 
<?php echo $this->Form->input('lottery', array('label' => 'Lottery Date')); ?> 
<?php echo $this->Form->end('Save Dates'); ?> 
</div> 
<?php echo $this->Html->script(array('Schools/add.js')); ?> 

這裏是數據庫:

CREATE TABLE IF NOT EXISTS `dates` (
    `id` int(10) NOT NULL AUTO_INCREMENT, 
    `schoolid` int(2) NOT NULL, 
    `grade` int(2) NOT NULL, 
    `startreg` datetime NOT NULL, 
    `endreg` datetime NOT NULL, 
    `lottery` datetime NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; 

如果任何人有任何想法爲什麼會發生這種情況,或者我可以採取什麼樣的調試步驟來解決這個問題,我將不勝感激!

+0

顯示了這陣$這個 - >請求 - >數據什麼的var_dump? –

回答

1

看起來你沒有正確創建你的表單。您正試圖創建'日期'。它應該是'日期'。這給一展身手:

<?php echo $this->Form->create('Date'); ?> 
+0

老兄你的我的英雄。 – Sugitime

+0

洛爾茲。隊友的歡呼聲。 –

0

我知道後是舊的,但對於那些誰仍然在此着陸:

有時形式與PUT請求自動提交和$這個 - >請求 - >是(」後')將返回false。

正確的測試將是:

if ($this->request->is('post') || $this->request->is('put')) { ... }