模型未保存到数据库

时间:2012-08-01 15:34:25

标签: php cakephp save

    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");
                }
            }
        }

问题是该模型似乎已保存,我按预期重定向,但它没有插入数据库。

使用debug(Model)我看到模型正在使用的id递增(就像插入然后删除一样)。

我使用了sql_dump - 没有INSERT的痕迹。

当然,没有验证错误。

我错过了什么?

2 个答案:

答案 0 :(得分: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方法  *  * @return void  * /

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 :(得分:-1)

$this->WebSubject->save();
由于没有数据,

不会保存任何内容! (编辑:这句话在评论中说错了。)

把:

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

save() function docs