2013-12-16 1 views
1

Когда я пытаюсь включить новый контент, в этом контроллере он не запускает параметр сохранения и возвращает сообщение об ошибке, так как я использовал функцию print_r для проверки $ this -> request-> и передает правильные параметры. Ниже приведен код:Метод добавления в CakePHP 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 
+0

Что такое сообщение об ошибке? Неудачно ли это при проверке? Сбой при сохранении/записи? (Работает ли ваша БД и работает ли она с существующей и правильно настроенной таблицей?) –

+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' 
      ) 
     ) 
    ); 
} 
?> 

Посмотреть/Canvas/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

Я не совсем уверен, что не так с вашим кодом. Тем не менее, как начать, вы должны попробовать следующее:

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();, если вы все еще есть проблемы, то проблема может быть в таблице .. также проверить таблицу dataabase ..