Yii框架学习 3-2 数据库操作--ActiveRecord 操作和生命周期

 

 

1、什么是ActiveRecord

 

2、声明AR类

 

3、 操作CRUD

 

    public function getPoststatus(){
$poststatus = Poststatus::find()->select(['name', 'id'])->indexBy('id')->column();

// $poststatus = Poststatus::find()->asArray()->all();
// $poststatus = ArrayHelper::map($poststatus, 'id', 'name');
return $poststatus;
}


直接将结果写在form中了
<?= $form->field($model, 'status')->dropDownList(
[Poststatus::find()->select(['name', 'id'])->indexBy('id')->column()],
['prompt'=>'请选择状态']
) ?>



二、生命周期

 

 

 

 

 2.1  生命周期中的方法重写   

save方法生命周期中所执行的方法 如: beforeSave

    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            if($insert){
                $this->create_time = time();
                $this->update_time = time();
            }else{
                $this->update_time = time();
            }

            return true;
        }else{
            return false;
        }
    }

 2-2 

 

 

 

 

posted @ 2020-04-18 12:08  没事就更  阅读(202)  评论(0编辑  收藏  举报