在 Yii 2.0 中报错:Calling unknown method yii2tech\\ar\\softdelete\\SoftDeleteBehavior::isTransactional() 的分析解决

1、在 Yii 2.0 中报错:Calling unknown method yii2tech\\ar\\softdelete\\SoftDeleteBehavior::isTransactional(),如图1

图1

{
    "name": "Exception",
    "message": "Calling unknown method: yii2tech\\ar\\softdelete\\SoftDeleteBehavior::isTransactional()",
    "code": 0,
    "type": "yii\\base\\UnknownMethodException",
    "file": "E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\BaseObject.php",
    "line": 222,
    "stack-trace": [
        "#0 [internal function]: yii\\base\\BaseObject->__call('isTransactional', Array)",
        "#1 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\Component.php(297): call_user_func_array(Array, Array)",
        "#2 E:\\wwwroot\\channel-pub-api\\vendor\\yii2tech\\ar-softdelete\\src\\SoftDeleteBehavior.php(447): yii\\base\\Component->__call('isTransactional', Array)",
        "#3 E:\\wwwroot\\channel-pub-api\\vendor\\yii2tech\\ar-softdelete\\src\\SoftDeleteBehavior.php(193): yii2tech\\ar\\softdelete\\SoftDeleteBehavior->isTransactional(4)",
        "#4 [internal function]: yii2tech\\ar\\softdelete\\SoftDeleteBehavior->softDelete()",
        "#5 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\Component.php(297): call_user_func_array(Array, Array)",
        "#6 E:\\wwwroot\\channel-pub-api\\common\\services\\NeteaseTpAppUserAccessTokenService.php(295): yii\\base\\Component->__call('softDelete', Array)",
        "#7 E:\\wwwroot\\channel-pub-api\\netease\\services\\NeteaseTpAppUserAccessTokenService.php(49): common\\services\\NeteaseTpAppUserAccessTokenService->softDelete(Object(netease\\models\\redis\\NeteaseTpAppUserAccessToken))",
        "#8 E:\\wwwroot\\channel-pub-api\\netease\\services\\NeteaseTpAppService.php(75): netease\\services\\NeteaseTpAppUserAccessTokenService->softDeleteByNeteaseTpAppUserId(2)",
        "#9 E:\\wwwroot\\channel-pub-api\\netease\\rests\\netease_tp_app_user\\DeleteAction.php(83): netease\\services\\NeteaseTpAppService->userSoftDelete(Object(common\\logics\\ChannelAppSource), Object(netease\\modules\\v1\\models\\NeteaseTpAppUser))",
        "#10 [internal function]: netease\\rests\\netease_tp_app_user\\DeleteAction->run('bc57d168bd9a11e...')",
        "#11 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\Action.php(94): call_user_func_array(Array, Array)",
        "#12 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\Controller.php(157): yii\\base\\Action->runWithParams(Array)",
        "#13 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\Module.php(528): yii\\base\\Controller->runAction('delete', Array)",
        "#14 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\web\\Application.php(103): yii\\base\\Module->runAction('v1/netease-tp-a...', Array)",
        "#15 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
        "#16 E:\\wwwroot\\channel-pub-api\\netease\\web\\index.php(17): yii\\base\\Application->run()",
        "#17 {main}"
    ]
}

2、查看代码,原因在于:操作 Redis 模型的删除使用了软删除(softDelete)

        /* 基于网易号的第三方内容平台应用的用户ID查找单个资源 */        $model = RedisNeteaseTpAppUserAccessToken::findOneByNeteaseTpAppUserId($neteaseTpAppUserId);
        if ($model->softDelete() !== false) {
            return ['status' => true, 'data' => $model];
        } else {
            throw new ServerErrorHttpException('Failed to soft delete the object (access token (Redis) for users of third-party content platform applications of Netease) for unknown reason.');
        }

3、编辑代码,操作 Redis 模型的删除使用硬删除(delete)

        /* 基于网易号的第三方内容平台应用的用户ID查找单个资源 */        $model = RedisNeteaseTpAppUserAccessToken::findOneByNeteaseTpAppUserId($neteaseTpAppUserId);
        if ($model->delete() !== false) {
            return ['status' => true, 'data' => $model];
        } else {
            throw new ServerErrorHttpException('Failed to delete the object (access token (Redis) for users of third-party content platform applications of Netease) for unknown reason.');
        }

4、总结:在 Yii 2.0 中,yii2tech\\ar\\softdelete\\SoftDeleteBehavior 不支持 Redis 的 AR 模型的软删除

永夜