2012-08-01 91 views
1
CREATE TABLE IF NOT EXISTS `web_subjects` (
     `id` int(11) NOT NULL AUTO_INCREMENT, 
     `web_subject_category_id` int(11) DEFAULT NULL, 
     `title` varchar(128) DEFAULT NULL, 
     `type` varchar(128) DEFAULT NULL, 
     `description` text, 
     `description_long` text, 
     `editable` int(1) DEFAULT NULL, 
     `deletable` int(1) DEFAULT NULL, 
     `published` int(1) DEFAULT NULL, 
     `order_number` int(11) DEFAULT NULL, 
     `created` timestamp NULL DEFAULT NULL, 
     `modified` timestamp NULL DEFAULT NULL, 
     PRIMARY KEY (`id`) 
    ) 
ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ; 

型號型號不保存到數據庫

class WebSubject extends AppModel 
    { 
     public $name = "WebSubject"; 

     public $belongsTo = array("WebSubjectCategory"); 

     public $validate = array(
      'title' => array(
       'rule' => 'notEmpty', 
       'message' => "Completati denumirea!" 
      ) 
     ); 

     public $hasMany = array(

      'Image' => array(
       'className' => 'WebFile', 
       'foreignKey' => 'entity_id', 
       'conditions' => array(
            'Image.type' => 'image', 
            'Image.entity_table_name' => 'web_subjects' 
           ),    
       'order'  => array('Image.order_number ASC', 'Image.id DESC'), 
       'dependent' => true 
      ), 
      'Video' => array(
       'className' => 'WebFile', 
       'foreignKey' => 'entity_id', 
       'conditions' => array(
            'Video.type' => 'video', 
            'Video.entity_table_name' => 'web_subjects' 
           ),    
       'order'  => array('Video.order_number ASC', 'Video.id DESC'), 
       'dependent' => true 
      ) 
     ); 
    } 

控制器動作

public function admin_page_add(){ 
      if(!empty($this->request->data)) 
      { 
       $this->WebSubject->create($this->data["WebSubject"]); 
       $this->WebSubject->type = 'page'; 
       //debug($this->WebSubject); 
       if($this->WebSubject->save()){ 
        //debug($this->WebSubject); 
        //die(0); 
        $this->Session->setFlash("Pagina a fost salvata!", "flash/simpla_success"); 
        $this->redirect('pages'); 
       } 
       else{ 
        $this->Session->setFlash("Pagina NU a fost salvata!", "flash/simpla_error"); 
       } 
      } 
     } 

的問題是,該模型appeares保存和我預期重定向,但它不會插入到數據庫中。

使用調試(型號),我看到該模型走的是ID遞增(像它插入,然後刪除)。

我使用了sql_dump--沒有INSERT的蹤跡。

而且,當然,沒有驗證錯誤。

我錯過了什麼?

回答

0

用戶這個

這是我的模型

<?php 
App::uses('AppModel', 'Model'); 
/** 
* CarModel Model 
* 
* @property Manufacturer $Manufacturer 
* @property Showroom $Showroom 
*/ 
class CarModel extends AppModel { 
/** 
* Display field 
* 
* @var string 
*/ 
    public $displayField = 'model'; 
/** 
* Validation rules 
* 
* @var array 
*/ 
    public $validate = array(
     'manufacturer_id' => array(
      'numeric' => array(
       'rule' => array('numeric'), 
       //'message' => 'Your custom message here', 
       //'allowEmpty' => false, 
       //'required' => false, 
       //'last' => false, // Stop validation after this rule 
       //'on' => 'create', // Limit validation to 'create' or 'update' operations 
      ), 
     ), 
     'model' => array(
      'notempty' => array(
       'rule' => array('notempty'), 
       //'message' => 'Your custom message here', 
       //'allowEmpty' => false, 
       //'required' => false, 
       //'last' => false, // Stop validation after this rule 
       //'on' => 'create', // Limit validation to 'create' or 'update' operations 
      ), 
     ), 
    ); 

    //The Associations below have been created with all possible keys, those that are not needed can be removed 

/** 
* belongsTo associations 
* 
* @var array 
*/ 
    public $belongsTo = array(
     'Manufacturer' => array(
      'className' => 'Manufacturer', 
      'foreignKey' => 'manufacturer_id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '' 
     ) 
    ); 

/** 
* hasMany associations 
* 
* @var array 
*/ 
    public $hasMany = array(
     'Showroom' => array(
      'className' => 'Showroom', 
      'foreignKey' => 'car_model_id', 
      'dependent' => false, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'exclusive' => '', 
      'finderQuery' => '', 
      'counterQuery' => '' 
     ), 
       'RequestCar' => array(
      'className' => 'RequestCar', 
      'foreignKey' => 'car_model_id', 
      'dependent' => false, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'exclusive' => '', 
      'finderQuery' => '', 
      'counterQuery' => '' 
     ) 
    ); 

} 
?> 

我控制器

/** * 方法admin_add * * @返回無效 */

public function admin_add() { 
     $this->layout = 'admin_layout'; 
     if ($this->request->is('post')) { 
      $this->CarModel->create(); 
      if ($this->CarModel->save($this->request->data)) { 
       $this->Session->setFlash(__('The car model has been saved')); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The car model could not be saved. Please, try again.')); 
      } 
     } 
     $manufacturers = $this->CarModel->Manufacturer->find('list'); 
     $this->set(compact('manufacturers')); 
    } 
-1
$this->WebSubject->save(); 

節省什麼,因爲沒有數據! (編輯:在評論說,這種說法是錯誤的。)

地說:

$this->WebSubject->save($this->request->data); 

save() function docs

+0

這是不對的。他將數據保存在'$ this-> WebSubject-> create()'語句中。您不*有*將數據傳遞給保存功能。 – jeremyharris 2012-08-01 15:52:28