【问题标题】:MODx second runProcessor returns response of the first oneMODx 第二个 runProcessor 返回第一个的响应
【发布时间】:2014-11-21 00:18:35
【问题描述】:

您好,提前感谢您的帮助! 所以问题是: 我在插件中运行 2 个不同的处理器 - 我正在创建一个用户(安全/用户/创建)并创建附加信息对象(我的自定义类)。 问题是第二个处理器总是返回第一个处理器的响应。如果我删除第一个处理器调用 - 没关系。当我尝试在第二个处理器本身中做同样的事情时,问题是一样的。所以代码:

$response = $modx->runProcessor('security/user/create',$_REQUEST);
$resp=$modx->runProcessor('mgr/userdata/create',$_REQUEST,array("processors_path"=>$custom_path));
$modx->log(MODx::LOG_LEVEL_ERROR,print_r($resp->response,true));

返回:

[2014-11-21 01:01:44](错误@/index.php)数组 ( [成功] => [留言] => [总计] => 1 [错误] => 数组 ( [0] => 数组 ( [id] => 用户名 [msg] => 这个用户名已经被使用了! ) ) [对象] => 数组 ( ) )

这是一种什么样的巫术,如何让它发挥作用?

【问题讨论】:

    标签: php modx-revolution


    【解决方案1】:

    MODX 的处理器使用通用的$modx->error 对象。在日志中,我们在用户创建时出错。第二个处理器捕获它,无法成功完成。

    这完全是因为 $modx->error 对于一个流程中的所有处理器都是常见的。最简单的方法是在第一次调用处理器后使用$modx->error->reset

    但是如果我们更深入呢?

    您想为他创建用户和相关数据吗?好的。但是,如果用户创建成功而您的自定义数据创建失败怎么办?最后我们有不一致的数据。真是让人头疼。

    对我来说,最好的方法是创建扩展 security/user/create processor 的自定义处理器。有特殊的beforeSave 方法。因此,您可以了解用户创建成功的时间,然后创建您的自定义数据。这真是太棒了。 示例(它是我的一个项目中的处理器,与用户创建无关。但含义相同):

    class modWebOrdersOrdersBulkComplectationCompleteProcessor extends modWebOrdersOrdersUpdateProcessor{
    
    
    public function beforeSave(){
    
        $canSave = parent::beforeSave();
        if($canSave !== true){
            return $canSave;
        }    
    
        // if we are here user creating has no errors.
    
        // method for my additional logic. If i have errors there user won't be created
        $ok = $this->transactionCreate();
        if($ok !== true){
            return $ok;
        }
    
    
        return true;
    }
    
    protected function transactionCreate(){          
    
        // i'm usually works with related objects and use especially aggregates/composites relations. So if user has related data profile it will be saved when `save` method will be called for $this->object.
    
        $_Transaction = $this->modx->newObject('BillingProductTransaction', array(
            'createdby' => $this->modx->user->id,
            'cause'=>   2
        ));
    
        $this->object->Transaction = $_Transaction;
    
        // but anyway you can run your subprocessor there. But there are some cons
        // 1. $this->modx->error is common. if you make multiple runProcessor call there you can get some weird problems with logic. (error->reset() is hack not feature)
        // 2. when your project architecture grows logic based on relations becomes more maintainable.
    
        return true;
    }
    

    【讨论】:

      【解决方案2】:

      您确定它会进入第二个处理器吗?尝试同时记录:

      $response = $modx->runProcessor('security/user/create',$_REQUEST);
      $modx->log(MODx::LOG_LEVEL_ERROR,print_r($response->response,true));
      
      $resp=$modx->runProcessor('mgr/userdata/create',$_REQUEST,array("processors_path"=>$custom_path));
      $modx->log(MODx::LOG_LEVEL_ERROR,print_r($resp->response,true));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-24
        • 2020-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-14
        相关资源
        最近更新 更多