diff --git a/src/Database/Relations/BelongsToMany.php b/src/Database/Relations/BelongsToMany.php index 373f3fcf5..29863be9c 100644 --- a/src/Database/Relations/BelongsToMany.php +++ b/src/Database/Relations/BelongsToMany.php @@ -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. */ @@ -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(); } /** diff --git a/src/Database/Relations/DefinedConstraints.php b/src/Database/Relations/DefinedConstraints.php index 50e3e8af5..4272229ad 100644 --- a/src/Database/Relations/DefinedConstraints.php +++ b/src/Database/Relations/DefinedConstraints.php @@ -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().'.*'); } } diff --git a/tests/Database/RelationsTest.php b/tests/Database/RelationsTest.php index 2d1ef9ec4..35da6bbc0 100644 --- a/tests/Database/RelationsTest.php +++ b/tests/Database/RelationsTest.php @@ -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(); }); } @@ -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([ @@ -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, ], ]; } @@ -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"', ], ]; }