Yii2:我可以创建删除模型时适用的规则和自定义错误消息吗?
Yii2: Can I create rules and custom error messages that apply when DELETING a model?
我想删除一个模型,但它可能在另一个模型中有相关记录,何时会禁止我这样做。我怎样才能最好地使用已经定义的关系来检查删除是否成功?也可能存在不允许删除的非关系原因。
一旦我确定了我的错误消息,我怎样才能最好地存储它们并将它们传递到前端? beforeDelete()
仅 returns true 或 false 但我当然需要向用户提供友好的错误消息,说明为什么无法删除记录...
已经定义的关系例如:
public function getPhonenumbers() {
return $this->hasMany(Phonenumber::class, ['ph_contactID' => 'contactID']);
}
您可以在 beforeDelete()
中抛出带有错误消息的异常,并在控制器中捕获它。
public function beforeDelete() {
if ($this->getPhonenumbers()->exist()) {
throw new DeleteFailException('Records with phone numbers cannot be deleted.');
}
return parent::beforeDelete();
}
并且在控制器操作中:
try {
$model->delete();
} catch (DeleteFailException $esception) {
Yii::$app->session->setFlash('error', $exception->getMessage());
}
从@vishuB 和@rob006 的回答中,我想到了自己的解决方案,我认为这会更好,因为我可以提供多个错误消息,它可以在 API 中使用好吧,它不依赖于 try/catching 异常:
public function beforeDelete() {
if (!parent::beforeDelete()) {
$this->addError('contactID', 'For some unknown reason we could not delete this record.');
}
if (!empty($this->phonenumbers)) {
$this->addError('contactID', 'Contact is linked to a used phone number and cannot be deleted.');
}
if ($some_other_validation_fails) {
$this->addError('contactID', 'Contact cannot be deleted because it is more than two months old.');
}
return ($this->hasErrors() ? false : true);
}
然后在我的行动中我这样做:
$contact = Contact::findOne($contactID);
if (!$contact->delete()) {
return $contact->getErrors(); //or use flash messages and redirect to page if you prefer
}
return true;
我想删除一个模型,但它可能在另一个模型中有相关记录,何时会禁止我这样做。我怎样才能最好地使用已经定义的关系来检查删除是否成功?也可能存在不允许删除的非关系原因。
一旦我确定了我的错误消息,我怎样才能最好地存储它们并将它们传递到前端? beforeDelete()
仅 returns true 或 false 但我当然需要向用户提供友好的错误消息,说明为什么无法删除记录...
已经定义的关系例如:
public function getPhonenumbers() {
return $this->hasMany(Phonenumber::class, ['ph_contactID' => 'contactID']);
}
您可以在 beforeDelete()
中抛出带有错误消息的异常,并在控制器中捕获它。
public function beforeDelete() {
if ($this->getPhonenumbers()->exist()) {
throw new DeleteFailException('Records with phone numbers cannot be deleted.');
}
return parent::beforeDelete();
}
并且在控制器操作中:
try {
$model->delete();
} catch (DeleteFailException $esception) {
Yii::$app->session->setFlash('error', $exception->getMessage());
}
从@vishuB 和@rob006 的回答中,我想到了自己的解决方案,我认为这会更好,因为我可以提供多个错误消息,它可以在 API 中使用好吧,它不依赖于 try/catching 异常:
public function beforeDelete() {
if (!parent::beforeDelete()) {
$this->addError('contactID', 'For some unknown reason we could not delete this record.');
}
if (!empty($this->phonenumbers)) {
$this->addError('contactID', 'Contact is linked to a used phone number and cannot be deleted.');
}
if ($some_other_validation_fails) {
$this->addError('contactID', 'Contact cannot be deleted because it is more than two months old.');
}
return ($this->hasErrors() ? false : true);
}
然后在我的行动中我这样做:
$contact = Contact::findOne($contactID);
if (!$contact->delete()) {
return $contact->getErrors(); //or use flash messages and redirect to page if you prefer
}
return true;