Skip to content

Commit

Permalink
Multiple fixes related to PR #543 (#550)
Browse files Browse the repository at this point in the history
* return a collection, not an object

* use fully qualified key names to avoid conflicts

* add pivot data test; add timestamps

* override sync() method in order to flush query cache
  • Loading branch information
mjauvin authored Feb 3, 2021
1 parent 642f597 commit 1fa47b3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/Database/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ public function save(Model $model, array $pivotData = [], $sessionKey = null)
return $model;
}

/**
* Override sync() method of BelongToMany relation in order to flush the query cache.
* @param array $ids
* @param bool $detaching
* @return array
*/
public function sync($ids, $detaching = true)
{
parent::sync($ids, $detaching);
$this->flushDuplicateCache();
}

/**
* Create a new instance of this related model with deferred binding support.
*/
Expand Down Expand Up @@ -160,7 +172,7 @@ public function detach($ids = null, $touch = true)
{
$attachedIdList = $this->parseIds($ids);
if (empty($attachedIdList)) {
$attachedIdList = $this->allRelatedIds();
$attachedIdList = $this->allRelatedIds()->all();
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Database/Relations/DefinedConstraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public function newPivotQuery()
// add relation's conditions and scopes to the query
$this->addDefinedConstraintsToQuery($query);

return $query->join($this->related->getTable(), $this->relatedPivotKey, '=', $this->relatedKey);
$related = $this->getRelated();

return $query
->join($related->getTable(), $related->getQualifiedKeyName(), '=', $this->getOtherKey())
->select($this->getTable().'.*');
}
}
40 changes: 37 additions & 3 deletions tests/Database/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function createTables()
$table->primary(['post_id', 'term_id']);
$table->unsignedInteger('post_id');
$table->unsignedInteger('term_id');
$table->string('data')->nullable();
$table->timestamps();
});
}

Expand Down Expand Up @@ -170,6 +172,19 @@ public function testBelongsToManyDetachOneCategory()
$this->assertEquals(3, $post->terms()->count());
}

public function testPivotData()
{
$data = 'My Pivot Data';
$post = Post::first();

$id = $post->categories()->get()->last()->id;
$updated = $post->categories()->updateExistingPivot($id, [ 'data' => $data ]);
$this->assertTrue($updated === 1);

$category = $post->categories()->find($id);
$this->assertEquals($data, $category->pivot->data);
}

public function testTerms()
{
$post = Post::create([
Expand Down Expand Up @@ -241,26 +256,37 @@ class Post extends \October\Rain\Database\Model

public $fillable = ['title'];

protected $dates = [
'created_at',
'updated_at',
'episode_at'
];

public $belongsToMany = [
'tags' => [
Term::class,
'table' => 'posts_terms',
'key' => 'post_id',
'otherKey' => 'term_id',
'conditions' => 'type = "tag"'
'pivot' => ['data'],
'timestamps' => true,
'conditions' => 'type = "tag"',
],
'categories' => [
Term::class,
'table' => 'posts_terms',
'key' => 'post_id',
'otherKey' => 'term_id',
'conditions' => 'type = "category"'
'pivot' => ['data'],
'timestamps' => true,
'conditions' => 'type = "category"',
],
'terms' => [
Term::class,
'table' => 'posts_terms',
'key' => 'post_id',
'otherKey' => 'term_id',
'timestamps' => true,
],
];
}
Expand All @@ -271,13 +297,21 @@ class Term extends \October\Rain\Database\Model

public $fillable = ['type', 'name'];

protected $dates = [
'created_at',
'updated_at',
'episode_at'
];

public $belongsToMany = [
'posts' => [
'Post',
'table' => 'posts_terms',
'key' => 'term_id',
'otherKey' => 'post_id',
'conditions' => 'type = "post"'
'pivot' => ['data'],
'timestamps' => true,
'conditions' => 'type = "post"',
],
];
}
Expand Down

0 comments on commit 1fa47b3

Please sign in to comment.