1.sql方法
$hasCount = User::find()
->where(['or', ['name' => $name], ['code' => $code]])
->andWhere(['!=', 'id', $id])
->andWhere(['isDelete' => '0'])
->count();
if ($hasCount) {
return JsonService::responseError("已经存在");
}
2.yii场景方法
//场景的设置 会执行这个场景
$model->scenario = 'unique';
//rule的规则里面
[['name'], 'unique', 'filter' => function ($query) {
return $query->andWhere(['status' => 'A']);
}, 'on' => 'unique']
$query是一个查询的对象 这里是一个虚参