dtr87341 2014-10-23 15:48
浏览 52
已采纳

在cakephp中添加功能突然停止工作

I'm new to cakePHP, so I may be missing something obvious.

I have an add form that was working and saving to the database, and suddenly stopped. I didn't think I added anything significant, mostly just styling stuff...unfortunately my last backup was before I had finished the add function, so I don't have a way to go back to when it worked. If anyone can take a look and see where I'm going wrong, I would appreciate it!

My Task model:

App::uses('AuthComponent', 'Controller/Component');

class Task extends AppModel {

public $belongsTo = 'User';

public $validate = array(
    'task_name' => array(
        'custom' => array(
            'rule' => array('custom', '/^[a-z0-9 ]*$/i'),
            'required' => true,
            'message' => 'This field accepts letters and numbers only.'
        ),
        'maxLength' => array(
            'rule' => array('maxLength', 50),
            'message' => 'Task name cannot exceed 50 characters'
        )
    ),
    'frequency' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'allowEmpty' => true
        )
    ),
    'day' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'allowEmpty' => true
        )
    ),
    'user_id' => array(
        'numeric' => array(
            'rule' => 'numeric'
        )
    ),
    'month_type' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'required' => false
        )
    ),
    'month_number' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'required' => false
        )
    ),
    'month_day' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'allowEmpty' => true
        )
    ),
    'day_number' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'required' => false
        )
    )
);
}

The add function from my TasksController file:

public function add() {
    if ($this->request->is('post')) {   
        $this->Task->create();
        if ($this->Task->save($this->request->data)) {
            $this->Session->setFlash(__('Task saved!'));
            $this->redirect(array('action' => 'index'));
        } 
        else {
            $this->Session->setFlash(__('The task could not be saved'));
        }   
    }
}

And the form from my tasks/add.ctp file:

<?php 
echo $this->Form->create('Task',array('class' => 'taskForm'));
$userId = $this->Session->read('Auth.User.id');?>

<fieldset>
    <legend class="welcomeText"><?php echo __('Add a Task'); ?></legend>
    <?php 

        echo $this->Form->hidden('user_id', array('value' => $userId));

        echo $this->Form->input('task_name', array('label' => 'Task Name', 'maxLength' => 50));

        //set frequency
        echo "<p>How often should this task be done?</p>";
        $options = array('unset' => 'Decide Later','daily' => 'Daily','weekly' => 'Weekly','monthly' => 'Monthly');
        $attributes = array('value' => 'unset','separator' => '<br/>','class' => 'frequencyRadio','legend' => false);
        echo $this->Form->radio('frequency', $options, $attributes);

        //optional answers

        //if "weekly" is selected
        echo "<div id=\"weeklyRadio\" >";
        echo "<p>Set a day of the week for this task?</p>";
        $options = array('unset' => 'Decide later','monday' => 'Monday','tuesday' => 'Tuesday','wednesday' => 'Wednesday',
        'thursday' => 'Thursday','friday' => 'Friday','saturday' => 'Saturday','sunday' => 'Sunday');
        $attributes = array('value' => 'unset','separator' => '<br/>','class' => 'weeklyRadio','legend' => false);
        echo $this->Form->radio('day',$options,$attributes);
        echo "</div>"; 

        //if "monthly" is selected
        ?>
        <div id="monthlyRadio">
            <p>Schedule this task?</p>
            <input type="radio" name="data[Task][month_type]" id="TaskMonthTypeUnset" 
                value="unset" class="monthlyRadio" required="required" checked="checked" />
            <label for="TaskMonthTypeUnset">Decide later</label><br/>
            <input type="radio" name="data[Task][month_type]" id="TaskMonthTypeNumber" 
                value="number" class="monthlyRadio" required="required" />
            <label for="TaskMonthTypeNumber">
                <?php

                    echo "On the ";
                    $options = array(1 => '1st',2 => '2nd',3 => '3rd',4 => '4th',5 => '5th',6 => '6th',7 => '7th',8 => '8th',9 => '9th',
                            10 => '10th',11 => '11th',12 => '12th',13 => '13th',14 => '14th',15 => '15th', 16 => '16th',
                            17 => '17th',18 => '18th',19 => '19th',20 => '20th',21 => '21st',22 => '22nd',23 => '23rd',
                            24 => '24th',25 => '25th',26 => '26th',27 => '27th',28 => '28th',29 => '29th',30 => '30th',31 => '31st');                       
                    echo $this->Form->select('month_number',$options,array('value' => null));
                    echo " of the month";
                ?>
            </label><br/>
            <input type="radio" name="data[Task][month_type]" id="TaskMonthTypeDay" value="day" class="monthlyRadio" required="required" />
            <label for="TaskMonthTypeDay">
                <?php

                    echo "On the ";
                    $options = array(1 => '1st',2 => '2nd',3 => '3rd',4 => '4th',5 => '5th',6 => 'last');
                    echo $this->Form->select('day_number',$options,array('value' => null));
                    echo "&nbsp";
                    $options = array('monday' => 'Monday','tuesday' => 'Tuesday','wednesday' => 'Wednesday',
                            'thursday' => 'Thursday','friday' => 'Friday','saturday' => 'Saturday','sunday' => 'Sunday');
                    echo $this->Form->select('month_day',$options,array('value' => null));
                    echo " of the month";
                ?>
            </label>
        </div>




    <?php 
    echo $this->Form->submit('Add Task', array('class' => 'formSubmit',  'title' => 'Create Task') ); 
?>
</fieldset>
<?php echo $this->Form->end(); ?>

There is also a javascript file that shows and hides the optional radio buttons, but I assume that wouldn't have anything to do with why it stopped saving to the database.

When I click the "Add Task" button on the form, it doesn't do anything at all (doesn't save to the database, and doesn't redirect to the index.ctp view like it used to). No idea what I'm missing here!

UPDATE

Fixed the problem! The places in my form where I specified a default value of null, it was passing an empty string. The data model was expecting numeric values for 'month_number' and 'day_number,' so it wasn't processing.

I fixed it by adding 0 => '' to the selection list, and specifying 0 as the default value, and now it works fine.

Thanks for the help!

  • 写回答

2条回答 默认 最新

  • doujuan2688 2014-10-23 19:59
    关注

    I don't see errors in your code. Add this in the controller add and make a debug for more information about what happens.

    public function add() {
        $this->loadModel('Task');
        debug($this->request->is('post')); //try this first
        //debug($this->request); //after try this
        if ($this->request->is('post')) {   
            $this->Task->create();
            if ($this->Task->save($this->request->data)) {
                $this->Session->setFlash(__('Task saved!'));
                $this->redirect(array('action' => 'index'));
            } 
            else {
                $this->Session->setFlash(__('The task could not be saved'));
            }   
        }
    

    }

    loadModel adds this. update your question with that display the debug, and I update mi answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)