Merge pull request #53 from cidosx/master

Improve DatabaseAdapter
This commit is contained in:
Jon 2022-11-03 23:16:36 +08:00 committed by GitHub
commit 774857a93c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 4 deletions

View File

@ -186,7 +186,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
*/
public function removePolicies(string $sec, string $ptype, array $rules): void
{
DB::transaction(function () use ($sec, $rules, $ptype) {
$this->eloquent->getConnection()->transaction(function () use ($sec, $rules, $ptype) {
foreach ($rules as $rule) {
$this->removePolicy($sec, $ptype, $rule);
}
@ -256,7 +256,11 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
foreach($oldRule as $k => $v) {
$instance->where('v' . $k, $v);
}
$instance->first();
$instance = $instance->first();
if (!$instance) {
return;
}
$update = [];
foreach($newPolicy as $k => $v) {
$update['v' . $k] = $v;
@ -276,7 +280,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
*/
public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): void
{
DB::transaction(function () use ($sec, $ptype, $oldRules, $newRules) {
$this->eloquent->getConnection()->transaction(function () use ($sec, $ptype, $oldRules, $newRules) {
foreach ($oldRules as $i => $oldRule) {
$this->updatePolicy($sec, $ptype, $oldRule, $newRules[$i]);
}
@ -296,7 +300,7 @@ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterCo
public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array
{
$oldRules = [];
DB::transaction(function () use ($sec, $ptype, $fieldIndex, $fieldValues, $newPolicies, &$oldRules) {
$this->eloquent->getConnection()->transaction(function () use ($sec, $ptype, $fieldIndex, $fieldValues, $newPolicies, &$oldRules) {
$oldRules = $this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
$this->addPolicies($sec, $ptype, $newPolicies);
});