CakePHP 2.0 不保存
所以我尝试用 CakePHP 保存表单。它的形式相当简单,但由于某种原因,永远不会生成 INSERT 语句。
这是控制器的片段:
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 ;
如果有人知道为什么会发生这种情况,或者我可以采取哪些调试步骤来尝试解决此问题,我将不胜感激它!
So I am trying to save a form with CakePHP. Its a fairly simple form, but for some reason, an INSERT statement is never being generated.
Here is a snippet from the Controller:
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);
}
Here is my Model:
<?php
class Date extends AppModel {
public $name = 'Date';
}
Here is the view:
<?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')); ?>
And here is the database:
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 ;
If anyone has any idea why this is happening, or what debug steps I can take to try to resolve this, I would appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您没有正确创建表单。您正在尝试创建“日期”。应为“日期”。试一试:
Looks like you're not creating your form correctly. You're attempting to create 'Dates'. It should be 'Date'. Give this a go:
我知道这篇文章很旧,但对于那些仍然登陆这里的人来说:
有时表单会自动使用 PUT 请求提交,并且 $this->request->is('post') 将返回 false。
正确的测试是:
I know the post is old but for those who still land here:
Sometimes the form is submitted with PUT request automatically and $this->request->is('post') will return false.
The correct test would be: