折雨的天空
一个诡异的Thinkphp8的验证场景的错误,报错内存溢出Allowed memory size of
2024-8-20 我好笨


代码:






public function sceneDelete()
{
return $this->only(['id'])->append('id', 'checkAbleDelete');
}



这是一个删除场景的验证,id自带验证规则如下:









protected $rule = [
'id' => 'require|checkCategory',
'name' => 'require|unique:'.ArticleCategory::class,
'status' => 'require|in:0,1',
];



验证方法:













protected  function checkCategory($value)
{
$category = ArticleCategory::findOrEmpty($value);
if ($category->isEmpty()) {
return '文章分类不存在';
}
return true;
}



另外一个方法:









protected function checkAbleDelete($value)
{
$hasArticle = ArticleInfo::where(['category_id' => $value])->findOrEmpty();
if (!$hasArticle->isEmpty()) {
return '该分类已被文章使用,暂不可删除';
}
unset($hasArticle);
$hasSon = ArticleCategory::where(['parent_id' => $value])->findOrEmpty();
if (!$hasSon->isEmpty()) {
return '该分类存在子分类,暂不可删除';
}
unset($hasSon);
$dept = ArticleCategory::findOrEmpty($value);
if ($dept['parent_id'] == 0) {
return '顶级分类不可删除';
}

    //罪魁祸首
return true;
}



问题就是,验证的时候try,catch会走catch,但是获取不到验证的错误信息,getTraceAsString可以获取到Allowed memory size of错误。






开了调试模式,看了apache日志,没有什么错误,始终不得其解。







最后原因,就是上面代码中的return true的那句话,由于验证方法不规范,验证通过没有返回true,导致内存移除。



排查的时候我把checkAbleDelete的内容全部注释完,都会报内存移除,切记要返回值。

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容