Skip to content

Commit

Permalink
Update ManyToMany.php
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Apr 2, 2024
1 parent ffa34d4 commit d5a21a5
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/mako/database/midgard/relations/ManyToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use mako\database\midgard\ResultSet;
use mako\database\query\Query;
use mako\database\query\Raw;
use RuntimeException;

use function array_combine;
use function array_diff;
Expand Down Expand Up @@ -352,16 +353,25 @@ public function unlink(mixed $id = null, array $attributes = []): bool
*/
public function synchronize(array $ids, array $attributes = []): bool
{
if (!empty($attributes) && array_is_list($attributes)) {
throw new RuntimeException('Synchronize can only be used with a single set of attributes.');
}

$success = true;

$keys = $this->getJunctionKeys($ids);

// Fetch existing links

$existing = $this->junction()
$query = $this->junction()
->where($this->getForeignKey(), '=', $this->origin->getPrimaryKeyValue())
->select([$this->getJunctionKey()])
->all()
->select([$this->getJunctionKey()]);

foreach ($attributes as $column => $value) {
$query->where($column, '=', $value);
}

$existing = $query->all()
->pluck($this->getJunctionKey());

// Link new relations
Expand Down

0 comments on commit d5a21a5

Please sign in to comment.