Yii记录没有插入DB

Zab*_*abs 2 database activerecord frameworks yii

下面是我的控制器和模型逻辑 - 我刚开始使用准系统Yii安装来玩它.

我没有错误,但没有看到数据库中的新条目 - 我的数据库已在main.php中配置(这可用作Gii运行).

// controllers/PageController.php
class PageController extends Controller
{
    public function actionSave($value='')
    {
        $pageObj = new Page;
        $pageObj->savePage();
    }     
}


// models/Page.php
Run Code Online (Sandbox Code Playgroud)

class Page extends CActiveRecord
{
/**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'page';
}

/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('title, date_updated', 'required'),
        array('live', 'numerical', 'integerOnly'=>true),
        array('user_id', 'length', 'max'=>10),
        array('title', 'length', 'max'=>100),
        array('content, date_published', 'safe'),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, user_id, live, title, content, date_updated, date_published', 'safe', 'on'=>'search'),
    );
}

/**
 * @return array relational rules.
 */
public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'comments' => array(self::HAS_MANY, 'Comment', 'page_id'),
        'user' => array(self::BELONGS_TO, 'User', 'user_id'),
        'files' => array(self::MANY_MANY, 'File', 'page_has_file(page_id, file_id)'),
    );
}

/**
 * @return array customized attribute labels (name=>label)
 */
public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'user_id' => 'User',
        'live' => 'Live',
        'title' => 'Title',
        'content' => 'Content',
        'date_updated' => 'Date Updated',
        'date_published' => 'Date Published',
    );
}

/**
 * Retrieves a list of models based on the current search/filter conditions.
 *
 * Typical usecase:
 * - Initialize the model fields with values from filter form.
 * - Execute this method to get CActiveDataProvider instance which will filter
 * models according to data in model fields.
 * - Pass data provider to CGridView, CListView or any similar widget.
 *
 * @return CActiveDataProvider the data provider that can return the models
 * based on the search/filter conditions.
 */
public function search()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id,true);
    $criteria->compare('user_id',$this->user_id,true);
    $criteria->compare('live',$this->live);
    $criteria->compare('title',$this->title,true);
    $criteria->compare('content',$this->content,true);
    $criteria->compare('date_updated',$this->date_updated,true);
    $criteria->compare('date_published',$this->date_published,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

/**
 * Returns the static model of the specified AR class.
 * Please note that you should have this exact method in all your CActiveRecord descendants!
 * @param string $className active record class name.
 * @return Page the static model class
 */
public static function model($className=__CLASS__)
{
    return parent::model($className);
}

public function savePage($value='')
{
    $page = new page;
    $model->isNewRecord = true;
    $model->primaryKey = NULL;
    $page->title='sample page';
    $page->content='content for the sample page';
    $page->save(false);
}
}
Run Code Online (Sandbox Code Playgroud)

Ali*_*our 8

在Yii中,当您要插入具有一些空列的表时,必须在规则中放置空列,SAFE如下所示:

array('primaryKey','safe'),
Run Code Online (Sandbox Code Playgroud)

现在,Yii知道这primaryKey是一个空列.因此,通过插入当前模型没有问题.

请注意,当您使用FALSE调用save()方法时,您告诉您的模型不要在插入时进行验证.

此外,跳过可能错误的正确方法是在插入之前验证您的模型,如下所示:

if($model->validate()){
    // VALIDATE, YOU CAN CALL SAVE FUNCTION
}else{
    //here you can send an error message via FLASH or you can debug what the exact error is like below:
    CVarDumper::dump($model->getErrors(),5678,true);
    Yii::app()->end();
}
Run Code Online (Sandbox Code Playgroud)

我希望,它有所帮助