MODx第二个runProcessor返回第一个响应
问题描述:
嗨,并提前感谢任何帮助! 所以问题是: 我在插件中运行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 ( ) )
什么样的巫术是它,以及如何使它工作?
答
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;
}
答
你确定它到了第二个处理器吗?尝试登录:
$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));