雄辩的更新模型模糊行为 [英] Eloquent update model ambiguous behavior
问题描述
我在修改个人资料/用户数据时遇到问题。我正在使用模板方法的责任设计模式链作为我在整个系统中的更新方案,以便更新过程在事务中开始,就像任何错误发生一切回滚一样。
abstract class UpdateProcessor
{
protected abstract function BeginTransaction();
protected abstract function Execute();
protected abstract function Commit();
protected abstract function Rollback();
公共函数ProcessUpdate()
{
尝试 {
$ this-> BeginTransaction() ;
$ this->执行();
$ this-> Commit();
} catch (\ Exception $ e){
$ this-> Rollback();
}
}
}
abstract class BaseProcessor扩展UpdateProcessor
{
公共函数BeginTransaction()
{
DB :: beginTransaction();
}
公共函数Commit()
{
DB :: commit();
}
公共函数Rollback()
{
DB :: rollBack();
}
}
class UserUpdateProcessor扩展BaseProcessor
{
private $ repository;
private $ reqs;
public function __construct(BaseRepository $ repo,$ reqsArray)
{
$ this-> repository = $回购;
$ this-> reqs = $ reqsArray;
}
public function Execute()
{
$ handler1 = new UpdateProfiler($ this-> repository);
$ handler2 = new UpdateAccount($ this-> repository-> helperRepo);
$ handler1-> nextHandler = $ handler2;
$ handler1-> process($ this-> reqs);
}
}
class UpdateProfiler扩展Handler
{
public $ nextHandler ;
private $ repository ;
private $ keys = [
' id',' user_login',' phone_login',' user_pass' ,' user_email',' notk'];
public function __construct(BaseRepository $ repo)
{
$ this-> repository = $ repo ;
}
公共函数处理($ reqsArray)
{
$这 - > repository->更新(array_diff_key($ reqsArray,array_flip($这 - > keys)),
$ reqsArray [$ this-> repository-> primaryKey]);
<跨度类= 代码关键字>如果跨度>(is_null($这 - >!nextHandler)及;&安培; $这 - > nextHandler的instanceof处理程序)
$ this-> nextHandler-> process($ reqsArray);
}
}
class UpdateAccount扩展Handler
{
public $ nextHandler ;
private $ repository ;
private $ keys = [
' id',' user_login',' phone_login',' user_pass' ,' user_email',' notk'];
public function __construct(BaseRepository $ repo)
{
$ this-> repository = $ repo ;
}
公共函数处理($ reqsArray)
{
$这 - > repository->更新(array_intersect_key($ reqsArray,array_flip($这 - > keys)),
$ reqsArray [$ this-> repository-> primaryKey]);
<跨度类= 代码关键字>如果跨度>(is_null($这 - >!nextHandler)及;&安培; $这 - > nextHandler的instanceof处理程序)
$ this-> nextHandler-> process($ reqsArray);
}
}
class DatabaseHelper
{
...
公共函数UpdateAccount()
{
$ manager = new AccountManager($ this-> repository,$ this-> reqs);
$ manager-> UpdateAccount();
}
}
公共函数更新(数组$ attributes,$ id)
{
...
$ model = $ this-> model-> findOrFail($ id);
$ model-> fill($ attributes);
$ model-> save();
}
一切正常,如果发生任何异常,一切都会回滚,但是,用户模型没有更新,当记录ORM发出的查询时,我发现这个
串(122) 更新`wp_users`设置`user_login` =?,`user_pass` =?,`user_email` =?,`notk` =?,`updated_at` =?其中`id`为null
array(5){
[0] =>
string(8)wp_admin
[1] =>
string(15)wp_wordpress123
[2] =>
string(31)amr.mohammad.rashad87@yahoo.com
[3] =>
string(16)clut0tLDj4ESXftv
[4] =>
string(19)2019-01-07 00:55:04
}
我不知道为什么要求没有空值的id
其中`id` 是 null
配置文件更新语句的工作方式相同,只是工作得很好,并且在where子句中它要求分析器ID。任何人都可以帮我解决这种模棱两可的行为!
我尝试了什么:
我试图谷歌解决几个小时的解决方案...
this-> BeginTransaction();
< BLOCKQUOTE>这 - >执行();
这 - > COMMIT();
} catch (\ Exception
I have an issue updating my profile/user data. I am using a mixed of template method\chain of responsibility design patterns to being my updating scenarios in the whole system so that the update process begin within a transaction as if any error occurred everything rolled back.
abstract class UpdateProcessor
{
protected abstract function BeginTransaction();
protected abstract function Execute();
protected abstract function Commit();
protected abstract function Rollback();
public function ProcessUpdate()
{
try {
$this->BeginTransaction();
$this->Execute();
$this->Commit();
} catch (\Exception $e) {
$this->Rollback();
}
}
}
abstract class BaseProcessor extends UpdateProcessor
{
public function BeginTransaction()
{
DB::beginTransaction();
}
public function Commit()
{
DB::commit();
}
public function Rollback()
{
DB::rollBack();
}
}
class UserUpdateProcessor extends BaseProcessor
{
private $repository;
private $reqs;
public function __construct(BaseRepository $repo, $reqsArray)
{
$this->repository = $repo;
$this->reqs = $reqsArray;
}
public function Execute()
{
$handler1 = new UpdateProfiler($this->repository);
$handler2 = new UpdateAccount($this->repository->helperRepo);
$handler1->nextHandler = $handler2;
$handler1->process($this->reqs);
}
}
class UpdateProfiler extends Handler
{
public $nextHandler;
private $repository;
private $keys = [
'id', 'user_login', 'phone_login', 'user_pass', 'user_email', 'notk'];
public function __construct(BaseRepository $repo)
{
$this->repository = $repo;
}
public function process($reqsArray)
{
$this->repository->update(array_diff_key($reqsArray, array_flip($this->keys)),
$reqsArray[$this->repository->primaryKey]);
if (!is_null($this->nextHandler) && $this->nextHandler instanceof Handler)
$this->nextHandler->process($reqsArray);
}
}
class UpdateAccount extends Handler
{
public $nextHandler;
private $repository;
private $keys = [
'id', 'user_login', 'phone_login', 'user_pass', 'user_email', 'notk'];
public function __construct(BaseRepository $repo)
{
$this->repository = $repo;
}
public function process($reqsArray)
{
$this->repository->update(array_intersect_key($reqsArray, array_flip($this->keys)),
$reqsArray[$this->repository->primaryKey]);
if (!is_null($this->nextHandler) && $this->nextHandler instanceof Handler)
$this->nextHandler->process($reqsArray);
}
}
class DatabaseHelper
{
...
public function UpdateAccount()
{
$manager = new AccountManager($this->repository, $this->reqs);
$manager->UpdateAccount();
}
}
public function update(array $attributes, $id)
{
...
$model = $this->model->findOrFail($id);
$model->fill($attributes);
$model->save();
}
Everything works fine and if any exception happens everything is rolled back, however, the user model is not updated and when logged the query issued by the ORM I find this
string(122) "update `wp_users` set `user_login` = ?, `user_pass` = ?, `user_email` = ?, `notk` = ?, `updated_at` = ? where `id` is null" array(5) { [0]=> string(8) "wp_admin" [1]=> string(15) "wp_wordpress123" [2]=> string(31) "amr.mohammad.rashad87@yahoo.com" [3]=> string(16) "clut0tLDj4ESXftv" [4]=> string(19) "2019-01-07 00:55:04" }
I do not know why it is asking for an id with no null value
where `id` is null
the profile update statement works the same way and it just working great and in the where clause it asks for the profiler id. Could anyone help me fixing this ambiguous behavior!
What I have tried:
I am trying to google for the solution for hours...
this->BeginTransaction();
this->Execute();
this->Commit(); } catch (\Exception
这篇关于雄辩的更新模型模糊行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!