2013-12-16 4 views
1

이 콘트롤러에 새 콘텐트를 포함하려고 할 때 그는 print 옵션을 사용하여 save 옵션을 실행하지 않고 오류 메시지를 반환합니다. -> 요청 -> 데이터, 그리고 올바른 매개 변수를 전달합니다.CakePHP 컨트롤러의 add 메소드가 작동하지 않습니다.

컨트롤러/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 
+0

오류 메시지가 무엇입니까? 검증 과정에서 실패 했습니까? 저장/쓰기 시간에 실패합니까? (DB가 정상적으로 실행되고 테이블이 이미 존재하고 있습니까?) –

+0

추가 기능에서 그는 $ this -> Canvas -> save ($ this -> request -> data)로 이동하지 않습니다 – MadsonJr

+0

어떻게 오류를 얻을 수 있습니까? ? – MadsonJr

답변

0

내가 해결

컨트롤러/CavasController.php :

<?php 
class CanvasController extends AppController { 
    public $name = 'Canvas'; 

    function beforeFilter() { 
     parent::beforeFilter(); 
     $this -> layout = 'dialog'; 
     $this -> Auth -> allow(); 
     //$this -> layout = 'default-original'; 
    } 

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

    public function setActiveCanvas($canvasId) { 
     $this -> Session -> write('activeCanvas', $canvasId); 
    } 

    public function add() { 
     if ($this -> request -> is('post')) { 
      $this -> Canvas -> create(); 
      if ($this -> Canvas -> save($this -> request -> data)) { 
       $this -> Session -> setFlash(__('The Canvas has been saved')); 
       $this -> redirect('/'); 
      } 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('/'); 
     } 
    } 

    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 : 여기에 코드입니다

,
<?php 
class Canvas extends AppModel { 
    public $name = 'Canvas'; 

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

    public $belongsTo = array(
     'Star' => array(
      'className'  => 'Stars', 
      'foreignKey' => 'stars_id' 
     ), 
     'User' => array(
      'className'  => 'Users', 
      'foreignKey' => 'users_id' 
     ), 
     'UserRole' => array(
      'className'  => 'UserRoles', 
      'foreignkey' => 'user_roles_id' 
     ) 
    ); 

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

보기/캔버스/add.ctp :

<?php echo $this -> Form -> create('Canvas',array('action'=>'add')); ?> 
<fieldset> 
    <?php echo $this -> Form -> input('Canvas.name'); ?> 
    <?php echo $this -> Form -> input('Canvas.description'); ?> 
</fieldset> 
<?php echo $this -> Form -> end(); ?> 

MySQL은 :

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 
2

코드에 어떤 문제가 있는지 잘 모르겠습니다. 그러나 시작으로 당신은 시도해야 다음 : 당신은 아직도 .. 또한 dataabase 테이블을 확인 후 문제가 테이블에있을 수 있습니다 문제가있는 경우

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.')); 
     } 
    } 
} 
0

$this->Canvas->create();을 추가하여 그것을 시도 ..