CakePHP 2.0 不保存

发布于 2024-12-17 04:01:10 字数 2514 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉絮湮 2024-12-24 04:01:10

看来您没有正确创建表单。您正在尝试创建“日期”。应为“日期”。试一试:

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

Looks like you're not creating your form correctly. You're attempting to create 'Dates'. It should be 'Date'. Give this a go:

<?php echo $this->Form->create('Date'); ?>
另类 2024-12-24 04:01:10

我知道这篇文章很旧,但对于那些仍然登陆这里的人来说:

有时表单会自动使用 PUT 请求提交,并且 $this->request->is('post') 将返回 false。

正确的测试是:

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

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:

if ($this->request->is('post') || $this->request->is('put')) { ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文