cakephp - CakePHP Controller 中的 add 方法不起作用

标签 cakephp addition cakephp-2.4

当我尝试在这个 Controller 中包含新内容时,他没有运行保存选项并返回错误消息,因为我使用 print_r 函数检查 $ this-> request-> data,并且正在传递正确的参数。下面是代码:

Controller /CanvasController.php:

class CanvasController extends AppController {
    public $name = 'Canvas';
    function beforeFilter() {
        parent::beforeFilter();
        //$this -> layout = 'dialog';
        $this -> layout = 'default-original';
    }

    public function index() {
        $this -> set('Canvas', $this -> Canvas -> find('all'));
    }

    public function add() {
        if ($this -> request -> is('post')) {
            if ($this -> Canvas -> save($this -> request -> data)) {
                $this -> Session -> setFlash(__('The Canvas has been saved'));
                $this -> redirect(array('action' => 'index'));
            } else {
                $this -> Session -> setFlash(__('The Canvas could not be saved. Please, try again.'));
            }
        }
    }

    public function edit($id = null) {
        $this -> Canvas -> id = $id;
        if ($this -> request -> is('get')) {
            $this -> request -> data = $this -> Canvas -> read();
        } else if ($this -> Canvas -> save($this -> request -> data)) {
            $this -> Session -> setFlash('Canvas updated.');
            $this -> redirect(array('controller' => 'user', 'action' => 'index'));
        }
    }

    public function delete($id = null) {
        if (!$this -> request -> is('post')) {
            throw new MethodNotAllowedException();
        }
        $this -> Canvas -> id = $id;
        if (!$this -> Canvas -> exists()) {
            throw new NotFoundException(__('Invalid.'));
        }
        if ($this -> Canvas -> delete($id)) {
            $this -> Session -> setFlash('The Canvas with id: ' . $id . ' has been deleted.');
            $this -> redirect(array('controller' => 'user', 'action' => 'index'));
        }
        $this -> Session -> setFlash(__('Canvas was deleted.'));
        $this -> redirect(array('controller' => 'user', 'action' => 'index'));
    }
}

模型/Canvas.php:

class Canvas extends AppModel {
    public $name = 'Canvas';

    public $hasAndBelongsToMany = array(
        'users' => array(
            'classname' => 'Users',
            'foreignkey' => 'canvas_id',
            'joinTable' => 'canvas_has_users'
        )
    );

    public $hasMany = array(
        'CanvasContents' => array(
            'classname' => 'CanvasContents',
            'foreignKey' => 'canvas_id'
        )
    );

    public $validate = array(
        'name' => array(
            'required' => array(
                'rule' => array(
                    'notEmpty'
                ),
                'message' => 'A name is required'
            )
        )
    );
}

查看/Canvas/add.ctp:

<?php echo $this -> Form -> create('Canvas'); ?>
<fieldset>
    <?php   echo $this -> Form -> input('name'); ?>
    <?php   echo $this -> Form -> input('description'); ?>
</fieldset>
<?php   echo $this -> Form -> end('Submit');?>

SQL 代码:

CREATE TABLE IF NOT EXISTS `sistema`.`canvas` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(45) NOT NULL,
    `description` TEXT NULL,
    PRIMARY KEY (`id`))
    ENGINE = InnoDB

最佳答案

我不完全确定您的代码有什么问题。但是,作为开始,您应该尝试以下操作:

public function add() {
    if ($this -> request -> is('post')) {
        $this->Canvas->create(); // **** ADD This missing call ****
        if ($this -> Canvas -> save($this -> request -> data)) {
            $this -> Session -> setFlash(__('The Canvas has been saved'));
            $this -> redirect(array('action' => 'index'));
        } else {
            $this -> Session -> setFlash(__('The Canvas could not be saved. Please, try again.'));
        }
    }
}

关于cakephp - CakePHP Controller 中的 add 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20612577/

相关文章:

Python:将输入中的奇数相加

shell - Cakephp 如何将 CakeEmail 导入 Shell

CakePHP 2.x : How to use different column names for the Created, 修改了自动化字段?

php - 使用 CakePHP 2.x 列出文件夹和子文件夹的内容

php - CakePHP 分页顺序不适用于 SELECT .. AS .. 字段

php - cakephp 使用乘法查找表

检查 unsigned long long 溢出

c++ - 检测 32 位双字 + 双字进位/C++

javascript - 在表单提交之前获取单选按钮的值

cakephp - 如何使用 CakePHP 的 HtmlHelper 创建安全链接 (https)