CakePHP 2.0 不保存
Posted
技术标签:
【中文标题】CakePHP 2.0 不保存【英文标题】:CakePHP 2.0 Not saving 【发布时间】:2011-11-19 16:59:25 【问题描述】:所以我想用 Cakephp 保存一个表单。它是一个相当简单的形式,但由于某种原因,永远不会生成 INSERT 语句。
这是来自控制器的 sn-p:
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 ;
如果有人知道为什么会发生这种情况,或者我可以采取哪些调试步骤来尝试解决这个问题,我将不胜感激!
【问题讨论】:
var_dump 用这个数组 $this->request->data 显示了什么? 【参考方案1】:看起来您没有正确创建表单。您正在尝试创建“日期”。它应该是“日期”。试一试:
<?php echo $this->Form->create('Date'); ?>
【讨论】:
【参考方案2】:我知道这篇文章已经过时了,但对于那些仍然登陆这里的人来说:
有时表单会自动提交 PUT 请求,$this->request->is('post') 会返回 false。
正确的测试是:
if ($this->request->is('post') || $this->request->is('put')) ...
【讨论】:
以上是关于CakePHP 2.0 不保存的主要内容,如果未能解决你的问题,请参考以下文章
cakephp 在不使用视图的情况下保存 HABTM 关系的数据