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

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

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

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

而且,当然,没有验证错误。

我错过了什么?

4

2 回答 2

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'));
    }
于 2012-08-01T15:38:55.183 回答
-1
$this->WebSubject->save();

没有数据,什么都不保存!(编辑:此声明是错误的,如评论中所述。)

放:

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

save() 函数文档

于 2012-08-01T15:41:57.367 回答