Yii2 日期条件(访问)
我试图设置一个条件,如果注册表具有与同一日期相同的图像,则无法保存Yii2 日期条件(访问),yii2,Yii2,我试图设置一个条件,如果注册表具有与同一日期相同的图像,则无法保存 public function validateVisit() { if (Visit::find()->where(['date_visit'=>$this->date_visit])->all()) { if (Visit::find()->where(['imagen_id'=>$this->imagen_id])->all()) {
public function validateVisit()
{
if (Visit::find()->where(['date_visit'=>$this->date_visit])->all()) {
if (Visit::find()->where(['imagen_id'=>$this->imagen_id])->all()) {
$this->addError('imagen_id', 'Already exists this visit within the range.');
}
}
}
在我的代码中,当输入的图像已经存在于数据库中时,它不会保存。在我的情况下,我要求您在与同一图像一起输入的相同日期已经存在时,不要保存该图像。您可以为此使用
unique
验证器
public function rules()
{
return [
[['imagen_id'], 'unique', 'targetAttribute' => ['imagen_id', 'date_visit']],
// ... other validations ...
];
}
规则定义中的第一项说明将设置错误的属性。targetAttribute
定义必须唯一的属性组合。在这种情况下,只有当imagen\u id
和date\u visit
属性组合不存在时,验证才会通过
请参阅有关的详细信息。好的。非常感谢。