Skip to content

Commit

Permalink
Fix #545
Browse files Browse the repository at this point in the history
- Added an exclude argument to deleteBy and deleteById
  • Loading branch information
oyeaussie committed Jul 8, 2024
1 parent 5f92acd commit 64b1f44
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions system/Base/Providers/DatabaseServiceProvider/Ff/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,20 +654,28 @@ function($content) use ($filePath, $data) {
return $this->data;
}

public function deleteBy(array $criteria, $deleteRelated = true, int $returnOption = Query::DELETE_RETURN_BOOL)
public function deleteBy(array $criteria, $deleteRelated = true, $relationsConditions = false, $exclude = [])
{
$query = $this->createQueryBuilder()->where($criteria)->getQuery();
$dataArr = $this->findBy($criteria);

$query->getCache()->deleteAllWithNoLifetime();
if (count($dataAr) > 0) {
foreach ($dataArr as $data) {
if (isset($data['id'])) {
if (!$this->deleteById($data['id'], $deleteRelated, $relationsConditions, $exclude)) {
return false;
}
}
}
}

return $query->delete($returnOption);
return true;
}

public function deleteById($id, $deleteRelated = true, $relationsConditions = false): bool
public function deleteById($id, $deleteRelated = true, $relationsConditions = false, $exclude = []): bool
{
$id = $this->checkAndStripId($id);

if ($deleteRelated && !$this->deleteRelated($id, $relationsConditions)) {
if ($deleteRelated && !$this->deleteRelated($id, $relationsConditions, $exclude)) {
return false;
} else {
$this->createQueryBuilder()->getQuery()->getCache()->deleteAllWithNoLifetime();
Expand All @@ -680,12 +688,12 @@ public function deleteById($id, $deleteRelated = true, $relationsConditions = fa
}
}

public function deleteRelated($id, $relationsConditions = false)
public function deleteRelated($id, $relationsConditions = false, $exclude = [])
{
$data = $this->findById((int) $id);

if (!$data) {
throw new InvalidArgumentException('Record with ID' . $id . ' not found!');
throw new InvalidArgumentException('Record with ID ' . $id . ' not found!');
}

if (is_string($this->storeSchema)) {
Expand All @@ -702,6 +710,10 @@ public function deleteRelated($id, $relationsConditions = false)
$relation = explode('|', $property['relation']);

if (count($relation) > 0) {
if (in_array($relation[0], $exclude)) {
continue;
}

if ($relation[1] === 'hasOne' || $relation[1] === 'hasMany') {
if ((in_array('hasParams', $relation) && $relationsConditions && count($relationsConditions) === 0) ||
(in_array('hasParams', $relation) && $relationsConditions && count($relationsConditions) > 0 && !isset($relationsConditions[$relation[0]]))
Expand Down

0 comments on commit 64b1f44

Please sign in to comment.