Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Yii2 日期条件(访问)_Yii2 - Fatal编程技术网

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
属性组合不存在时,验证才会通过


请参阅有关的详细信息。

好的。非常感谢。