2014-11-21 69 views
2

嗨,並提前感謝任何幫助! 所以問題是: 我在插件中運行2個不同的處理器 - 我創建一個用戶(安全/用戶/創建)和創建額外的信息對象(我的自定義類)。 問題是第二個處理器總是返回第一個響應。如果我刪除第一個處理器調用 - 沒關係。問題是一樣的,當我試圖做同樣在第二個處理器itself.So代碼:MODx第二個runProcessor返回第一個響應

$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] (ERROR @ /index.php) Array 
(
    [success] => 
    [message] => 
    [total] => 1 
    [errors] => Array 
     (
      [0] => Array 
       (
        [id] => username 
        [msg] => This username is already used! 
       ) 

     ) 

    [object] => Array 
     (
     ) 

)

什麼樣的巫術是它,以及如何使它工作?

回答

1

MODX上的處理器正在使用常見的$modx->error對象。在日誌中,我們在創建用戶時出錯。第二個處理器抓住它並且不能成功完成。

而這一切都是因爲$ modx->錯誤對於一個流程中的所有處理器都很常見。最簡單的方法是在第一次處理器調用後使用$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; 
} 
0

你確定它到了第二個處理器嗎?嘗試登錄:

$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)); 
相關問題