1. 程式人生 > >Yii框架基礎增刪查改

Yii框架基礎增刪查改

返回一條資料
Country::find()->one();
返回所有資料
Country::find()->all();
返回記錄的數量
$country =Country::find()->count();


$model = new Country(); 例項化模型層新增
$model->username = 'zhangsan'; 欄位username
$model->password = '123456';
$model->save(); 儲存
例項化修改
$country = Country::find()->where(['id'=>'2'])->one(); 查詢一條資料

$country->username = '張三';
$country->save();
例項化刪除
$country = Country::find()->where(['id'=>'8'])->one();
$country->delete();


使用createCommand()進行新增資料
Yii::$app->db->createCommand()->insert('country',['username'=>'value','password'=>'value'])->execute();
使用createCommand()修改

Yii::$app->db->createCommand()->update('content',['name'=>'value'],'id=1')->execute();
使用createCommand()刪除
Yii::$app->db->createCommand()->delete('country','id=9')->execute();

直接刪除
Country::deleteAll('id=7');
直接修改
Country::updateAll(['username'=>'root'],'id=3');