当我试图包含新内容时,在此控制器中,他没有运行保存选项并返回错误消息,因为我使用了print_r函数来检查$ this - >请求 - >数据,并传递正确的参数。下面是代码:CakePHP控制器中的添加方法不起作用
控制器/ 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'
)
)
);
}
检视/帆布/ 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
什么是错误讯息?验证失败吗?保存/写入时是否失败? (你的数据库是否已经正常运行并且已经配置好了表格) –
在添加函数中,他不会$ this - > Canvas - > save($ this - > request - > data) – MadsonJr
如何获取错误? – MadsonJr