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 に答える