没有不值得去解决的问题,也没有不值得去学习的技术!

Fixing the “Calling unknown method yii2tech\ar\softdelete\SoftDeleteBehavior::isTransactional()” Error in Yii 2.0

Error in Yii 2.0: Calling unknown method yii2techarsoftdeleteSoftDeleteBehaviorisTransactional()

1. In Yii 2.0, an error occurs: Calling unknown method yii2tech\\ar\\softdelete\\SoftDeleteBehavior::isTransactional(), as shown in Figure 1

Error in Yii 2.0: Calling unknown method yii2techarsoftdeleteSoftDeleteBehaviorisTransactional()
Figure 1
JSON

{
    "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. Reviewing the code, the cause is that the delete operation on the Redis model used soft deletion (softDelete)

PHP

        /* 基于网易号的第三方内容平台应用的用户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. Edit the code so that the delete operation on the Redis model uses hard deletion (delete)

PHP

        /* 基于网易号的第三方内容平台应用的用户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. Conclusion: In Yii 2.0, yii2tech\\ar\\softdelete\\SoftDeleteBehavior does not support soft deletion for Redis AR models

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系