Skip to content

Commit

Permalink
Fix belongsToMany sync issue
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiCollin committed Feb 12, 2016
1 parent 78924a6 commit 4f568f6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
11 changes: 5 additions & 6 deletions src/Commands/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ protected function postStoreProcess()
// Update any pivot tables that has been modified.
$aggregate->updatePivotRecords();

// Now we can sync the related collections
if ($this->aggregate->exists()) {
$this->aggregate->syncRelationships($foreignRelationships);
}

// Update any dirty relationship. This include relationships that already exists, have
// dirty attributes / newly created related entities / dirty related entities.
$dirtyRelatedAggregates = $aggregate->getDirtyRelationships();
Expand All @@ -145,7 +140,11 @@ protected function postStoreProcess()
$this->createStoreCommand($related)->execute();
}


// Now we can sync the related collections
if ($this->aggregate->exists()) {
$this->aggregate->syncRelationships($foreignRelationships);
}

// TODO be move it to the wrapper class
// so it's the same code for the entity builder
$aggregate->setProxies();
Expand Down
7 changes: 4 additions & 3 deletions src/Relationships/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,20 +619,21 @@ public function sync(array $entities)
*/
protected function detachExcept(array $entities = [])
{
tdd($this->relationName);
$query = $this->newPivotQuery();

// If id is empty, we'll simply skip that statement.
if (count($entities) > 0) {
$keys = $this->getKeys($entities);

$query->whereNotIn($this->otherKey, $keys);
}

$parentKey = $this->parentMap->getKeyName();

$query->where($this->foreignKey, '=', $this->parent->getEntityAttribute($parentKey));

$query->delete();

$query = $this->newPivotQuery();
}


Expand Down
3 changes: 2 additions & 1 deletion src/System/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,11 @@ protected function parseManyRelationship($relation)
if (!$value = $this->parseForCommonValues($relation)) {
return true;
}

if (is_array($value) || $value instanceof Collection) {
$this->needSync[] = $relation;
}

// If the relation is a proxy, we test is the relation
// has been lazy loaded, otherwise we'll just treat
// the subset of newly added items.
Expand Down
29 changes: 24 additions & 5 deletions tests/AnalogueTest/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Analogue\ORM\Repository;
use Analogue\ORM\Entity;
use Analogue\ORM\EntityCollection;
use Illuminate\Support\Collection;
use AnalogueTest\App\Permission;
use AnalogueTest\App\Role;

class RepositoryTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -114,28 +116,45 @@ public function testStoreEntityWithChangedMultiRelation()
$analogue = get_analogue();
$roleRepo = $analogue->repository('AnalogueTest\App\Role');
$permissionRepo = $this->getRepository();

$mapper = get_mapper(Permission::class);


// Add two roles to permission
$permission = $permissionRepo->find(14);


$permission->roles = new EntityCollection([
$roleRepo->find(1),
$roleRepo->find(2),
]);

$permissionRepo->store($permission);

$permission = $permissionRepo->find(14);
$this->assertEquals(2, count($permission->roles));


$role = new Role('A new one that did not exists');
$role2 = new Role('Another Role that did not exists');
$roleRepo->store([$role, $role2]);
$permission->roles = new Collection([
$roleRepo->find($role->id),
$roleRepo->find($role2->id),
]);
$permissionRepo->store($permission);
// Reload from repo
$permission = $permissionRepo->find(14);
$this->assertEquals(2, $permission->roles->count());


// Change permission to only have a single role
$permission->roles = new EntityCollection([
$permission->roles = new Collection([
$roleRepo->find(1),
]);

$permissionRepo->store($permission);

// Reload from repo
$permission = $permissionRepo->find(14);

//$permission->roles->count();
// Permission should only have one role
$this->assertEquals(1, count($permission->roles));
}
Expand Down

0 comments on commit 4f568f6

Please sign in to comment.