Skip to content

Commit ab31b06

Browse files
author
Pantea Marius-ciclistu
committed
POC for laravel/framework#31778 add consequence lockUpdate new feature and sync trait
1 parent 2b5381a commit ab31b06

File tree

2 files changed

+106
-8
lines changed

2 files changed

+106
-8
lines changed

src/Models/BaseModel.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,55 @@ public function unique($key = null, $strict = false): Collection
600600
};
601601
}
602602

603+
/**
604+
* Prevent updates
605+
* Note that relations can be loaded and updated during the lock
606+
*/
607+
public function lockUpdates(bool $checkDirty = true): bool
608+
{
609+
if (
610+
!$this->exists
611+
|| $this->tmpDirtyIfAttributesAreSyncedFromCashedCasts !== null
612+
|| ($checkDirty && $this->isDirty())
613+
) {
614+
return false;
615+
}
616+
617+
$this->tmpDirtyIfAttributesAreSyncedFromCashedCasts = [];
618+
619+
return true;
620+
}
621+
622+
/**
623+
* Unlock updates
624+
*
625+
* To reset the model's $attributes and get the changes from dirty applied during the lock use:
626+
*
627+
* if ($this->unlockUpdate()) {
628+
* $dirty = $this->getDirty();
629+
* $this->attributes = $this->original;
630+
* $this->classCastCache = [];
631+
* $this->attributeCastCache = [];
632+
* }
633+
*
634+
* Note that relations can be loaded during the lock
635+
*/
636+
public function unlockUpdate(): bool
637+
{
638+
if ($this->isUnlockedForUpdate()) {
639+
return false;
640+
}
641+
642+
$this->tmpDirtyIfAttributesAreSyncedFromCashedCasts = null;
643+
644+
return true;
645+
}
646+
647+
public function isUnlockedForUpdate(): bool
648+
{
649+
return $this->tmpDirtyIfAttributesAreSyncedFromCashedCasts !== [];
650+
}
651+
603652
/**
604653
* Get all of the current attributes on the model.
605654
* @param bool $withoutMergeAttributesFromCachedCasts

src/Models/ExcessiveSetOptimizerOnSaveTrait.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,55 @@ public function __call($method, $parameters)
4343
return parent::__call($method, $parameters);
4444
}
4545

46+
/**
47+
* Prevent updates
48+
* Note that relations can be loaded and updated during the lock
49+
*/
50+
public function lockUpdates(bool $checkDirty = true): bool
51+
{
52+
if (
53+
!$this->exists
54+
|| $this->tmpDirtyIfAttributesAreSyncedFromCashedCasts !== null
55+
|| ($checkDirty && $this->isDirty())
56+
) {
57+
return false;
58+
}
59+
60+
$this->tmpDirtyIfAttributesAreSyncedFromCashedCasts = [];
61+
62+
return true;
63+
}
64+
65+
/**
66+
* Unlock updates
67+
*
68+
* To reset the model's $attributes and get the changes from dirty applied during the lock use:
69+
*
70+
* if ($this->unlockUpdate()) {
71+
* $dirty = $this->getDirty();
72+
* $this->attributes = $this->original;
73+
* $this->classCastCache = [];
74+
* $this->attributeCastCache = [];
75+
* }
76+
*
77+
* Note that relations can be loaded during the lock
78+
*/
79+
public function unlockUpdate(): bool
80+
{
81+
if ($this->isUnlockedForUpdate()) {
82+
return false;
83+
}
84+
85+
$this->tmpDirtyIfAttributesAreSyncedFromCashedCasts = null;
86+
87+
return true;
88+
}
89+
90+
public function isUnlockedForUpdate(): bool
91+
{
92+
return $this->tmpDirtyIfAttributesAreSyncedFromCashedCasts !== [];
93+
}
94+
4695
/**
4796
* Get all of the current attributes on the model.
4897
* @param bool $withoutMergeAttributesFromCachedCasts
@@ -247,18 +296,18 @@ protected function performUpdate(Builder $query): bool
247296
// Once we have run the update operation, we will fire the "updated" event for
248297
// this model instance. This will allow developers to hook into these after
249298
// models are updated, giving them a chance to do any special processing.
250-
$dirty = $this->getDirtyForUpdate();
299+
if ([] === $dirty = $this->getDirtyForUpdate()) {
300+
return false;
301+
}
251302

252-
if ([] !== $dirty) {
253-
$this->setKeysForSaveQuery($query)->update($dirty);
303+
$this->setKeysForSaveQuery($query)->update($dirty);
254304

255-
$this->syncChanges();
305+
$this->syncChanges();
256306

257-
$this->tmpDirtyIfAttributesAreSyncedFromCashedCasts = null;
258-
$this->tmpOriginalBeforeAfterEvents = $this->attributes;
307+
$this->tmpDirtyIfAttributesAreSyncedFromCashedCasts = null;
308+
$this->tmpOriginalBeforeAfterEvents = $this->attributes;
259309

260-
$this->fireModelEvent('updated', false);
261-
}
310+
$this->fireModelEvent('updated', false);
262311

263312
return true;
264313
}

0 commit comments

Comments
 (0)