1、快捷批量操作
yii\db\ActiveRecord::updateAll($attributes, $condition = '') //将所有satus为2的记录的status更新为1 //update user set status = 1 where status = 2 eg: User::updateAll(['status' => 1], 'status = 2'); yii\db\ActiveRecord::updateAllCounters($counters, $condition = '') //所有用户的age字段加1 //update user set age = age + 1 eg: User::updateAllCounters(['age' => 1]); yii\db\ActiveRecord::deleteAll($condition = '') //删除所有status为3的记录 //delete from user where status = 3 eg: User::deleteAll('status = 3');
2、快捷查询findOne()和findAll()
findOne()和findAll()传入一个参数时默认为主键,否则须以键值对表示
// 查询pk值为10的客户 $customer = Customer::findOne(10); $customer = Customer::find()->where(['id' => 10])->one();
// 查询age为30,status为1的客户 $customer = Customer::findOne(['age' => 30, 'status' => 1]); $customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
// 查询pk为10的所有客户 $customers = Customer::findAll(10); $customers = Customer::find()->where(['id' => 10])->all();
// 查询pk值为10,11,12的客户 $customers = Customer::findAll([10, 11, 12]); $customers = Customer::find()->where(['id' => [10, 11, 12]])->all();
// 查询年龄为30,状态值为1的所有客户 $customers = Customer::findAll(['age' => 30, 'status' => 1]); $customers = Customer::find()->where(['age' => 30, 'status' => 1])->all();