您好,并预先感谢您的任何帮助!所以问题是:我在插件中运行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] =>此用户名已被使用! ) ) [对象] =>数组 ( ) )
这是什么巫术,如何使它起作用?
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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句