diff --git a/src/FieldFactory.php b/src/FieldFactory.php index 9af0ca3..14fd6b6 100644 --- a/src/FieldFactory.php +++ b/src/FieldFactory.php @@ -89,7 +89,7 @@ public function mapRegex(array $map) * * @return self */ - public function mapTypes(array $types) + public function mapTypes(array $map) { $this->mapTypes = $map + $this->mapTypes; diff --git a/src/Queries/Mysql/ExtendedSelectionTrait.php b/src/Queries/Mysql/ExtendedSelectionTrait.php index e4a99c7..b3c8e72 100644 --- a/src/Queries/Mysql/ExtendedSelectionTrait.php +++ b/src/Queries/Mysql/ExtendedSelectionTrait.php @@ -69,7 +69,7 @@ public function relatedWith(AbstractRow $row) return $this->by($relation[1], $row->id); case Scheme::HAS_MANY: - return $this->byId($relation[1], $row->{$relation[1]}); + return $this->byId($row->{$relation[1]}); case Scheme::HAS_MANY_TO_MANY: $this->from($relation[1]); diff --git a/src/Row.php b/src/Row.php index cba6282..f4fc21e 100644 --- a/src/Row.php +++ b/src/Row.php @@ -352,13 +352,6 @@ public function unrelateAll(Table $relationTable) $this->{$relation[1]} = null; $this->relations[$relationTable->name] = new NullValue(); - $cache = $row->getCache(); - - if (isset($cache[$table->name])) { - unset($cache[$table->name][$this->id]); - $row->setCache($cache); - } - return $this->save(); } @@ -383,8 +376,8 @@ public function unrelateAll(Table $relationTable) ->by($relation[2], $this->id) ->run(); - unset($this->relations[$relation[1]]); - unset($this->relations[$relationTable->name][$row->id]); + $this->relations[$bridge->name] = $bridge->createCollection(); + $this->relations[$relationTable->name] = $relationTable->createCollection(); } } } diff --git a/src/RowCollection.php b/src/RowCollection.php index 69f00f9..286a8af 100644 --- a/src/RowCollection.php +++ b/src/RowCollection.php @@ -142,7 +142,7 @@ public function __set($name, $value) } //Join the relations and rows - self::join($table, $this->rows, $row->getTable(), $row, $relation[1]); + self::join($table, $this->rows, $value->getTable(), $value, $relation[1]); $this->loadedRelations[] = $name; } diff --git a/src/Table.php b/src/Table.php index 5aa55b4..ca1194d 100644 --- a/src/Table.php +++ b/src/Table.php @@ -63,8 +63,7 @@ protected function init() /** * Store a row in the cache. * - * @param int $id - * @param Row $Row + * @param Row $row */ public function cache(Row $row) { diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index b5363a9..34f9959 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -56,8 +56,8 @@ public function testRelatedQuery() { $db = $this->db; - $post = $db->post->create(['id' => 1]); - $comment = $db->comment->create(['id' => 1]); + $post = $db->post->create(['id' => 1])->save(); + $comment = $db->comment->create(['id' => 1])->save(); $this->assertEquals( 'SELECT `category`.`id`, `category`.`name`, `category_post`.`post_id` FROM `category`, `category_post`, `post` WHERE (`category_post`.`category_id` = `category`.`id`) AND (`category_post`.`post_id` = `post`.`id`) AND (`post`.`id` IN (:post_id))', @@ -69,6 +69,13 @@ public function testRelatedQuery() (string) $post->comment() ); + $this->assertEquals( + 'SELECT `post`.`id`, `post`.`title` FROM `post` WHERE (`post`.`id` IS NULL) LIMIT 1', + (string) $comment->post() + ); + + $comment->relate($post); + $this->assertEquals( 'SELECT `post`.`id`, `post`.`title` FROM `post` WHERE (`post`.`id` = :id) LIMIT 1', (string) $comment->post()