禁用数据库中的错误

时间:2016-01-26 17:54:19

标签: php yii message

我有一个注册表单,我在那里进行验证,这不符合我的要求。用户选择他们的出生日期,但是,如果他们选择不存在的日期,例如。 2016-02-31将发生SQL错误:

  

完整性约束违规 - yii \ db \ IntegrityException

     

SQLSTATE [23000]:完整性约束违规:1048列   'BirthDate'不能为null正在执行的SQL是:INSERT INTO   `urUser`(`Login`,`Email`,`RulesAccept`,`Rel_Sex`,`Name`,   `BirthDate`,`Surname`,`PasswordHash`,`auth_key`,`status`,   `created_at`,`updated_at`)VALUES('ania','ania @ wp.pl',1,2,   'Ania',NULL,'Kowalska',   '$ 2Y $ $ 13 3N.16FSerWx6IEbT1OZjBOReMeiWslCj..fhqDvgePxqg3jZhdanG',   'mMZ-MyAGRSdzH_VhN0qT5UEKPjiPvw3h',10,1453830416,1453830416)

当我点击后退按钮(左箭头)时,我收到此错误:

  

Yii :: $ app-> getSession() - > setFlash('错误','日期不正确');

如何禁用此第一个错误并强制页面停留在我将显示消息的注册表单中。我尝试实施try / catch,但我不知道这是否适合这样做。

我使用此功能来重新输入数据:

public function getDate() {
        return $this->year . '-' . $this->month . '-' . $this->day;
    }

    public function getDbFormatedDate() {
    if (checkdate($this->month, $this->day, $this->year)){
    $dateDeadline = date_create($this->getDate());
    Yii::$app->session->setFlash('success', Yii::t('app', 'Udało się zarejestrować'));
   return date_format($dateDeadline, 'Y-m-d');
    }
      Yii::$app->getSession()->setFlash('error', 'Nieprawidłowa data');

}

我在函数singnup中调用了我的getDbFormatedDate(),所以如果!is_null很难在那里使用:

 public function signup()
    {
        if ($this->validate()) {
            $user = new User();
            $user->Login = $this->login;
            $user->Email = $this->email;
            $user->RulesAccept=1;
            $user->Rel_Sex = $this->sex;
            $user->Name = $this->name;
            $user->BirthDate = $this->getDbFormatedDate();
            $user->Surname = $this->surname;
            $user->setPassword($this->password);
            $user->generateAuthKey();
            if ($user->save()) {
                return $user;
            }
        }

        return false;
    }

1 个答案:

答案 0 :(得分:1)

更新

 public function signup()
 {
    if ($this->validate()) {
        $user = new User();
        $user->BirthDate = $this->getDbFormatedDate();

        if (is_null($user->BirthDate))
            return false;

        $user->Login = $this->login;
        $user->Email = $this->email;
        $user->RulesAccept=1;
        $user->Rel_Sex = $this->sex;
        $user->Name = $this->name;
        $user->Surname = $this->surname;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        if ($user->save()) {
            return $user;
        }
    }

    return false;
}

好的,你不需要if,就像这样:

public function getDate() {
    return $this->year . '-' . $this->month . '-' . $this->day;
}

public function getDbFormatedDate() {
    if (checkdate($this->month, $this->day, $this->year)){
        $dateDeadline = date_create($this->getDate());
        Yii::$app->session->setFlash('success', Yii::t('app', 'Udało się zarejestrować'));
        return date_format($dateDeadline, 'Y-m-d');
    }
    Yii::$app->getSession()->setFlash('error', 'Nieprawidłowa data');
    return null;
}

然后如果你调用getDbFormattedDate(),请执行以下操作:

if (!is_null($this->getDbFormatedDate())
    // Handle your database insert
相关问题