Yii框架基础增删查改
程序员文章站
2022-09-04 14:34:14
返回一条数据 Country::find()->one(); 返回所有数据 Country::find()->all(); 返回记录的数量 $country =Country::find()->count(); $model = new Country(); 实例化模型层新增 $model->use ......
返回一条数据
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');
上一篇: php+ajax 文件上传