diff --git a/src/Table.php b/src/Table.php index 79dc1f3..4304904 100644 --- a/src/Table.php +++ b/src/Table.php @@ -301,26 +301,27 @@ public function offsetGet($offset): ?Row * @param mixed $offset * @param mixed $value */ - public function offsetSet($offset, $value): Row + public function offsetSet($offset, $value): void { //Insert on missing offset if ($offset === null) { $value['id'] = null; - return $this->create($value)->save(); + $this->create($value)->save(); + return; } //Update if the element is cached and exists $row = $this->getCached($offset); if ($row) { - return $row->edit($value)->save(); + $row->edit($value)->save(); + return; } //Update if the element it's not cached if (!$this->isCached($row)) { - $this->update() - ->columns($value) + $this->update($value) ->where('id = ', $offset) ->run(); } diff --git a/tests/TableTest.php b/tests/TableTest.php index 947ba71..291284c 100644 --- a/tests/TableTest.php +++ b/tests/TableTest.php @@ -38,9 +38,10 @@ public function testArrayAccess() //Insert $db->post[] = ['title' => 'First post', 'isActive' => 1]; + $db->post->insert(['title' => 'Second post', 'isActive' => 1])->run(); $this->assertTrue(isset($db->post[1])); - $this->assertSame(1, $db->post->count()); + $this->assertSame(2, $db->post->count()); //Select $post = $db->post[1]; @@ -49,14 +50,16 @@ public function testArrayAccess() //Update $db->post[1] = ['title' => 'First post edited']; + $db->post[2] = ['title' => 'Second post edited']; $this->assertEquals('First post edited', $post->title); + $this->assertEquals('Second post edited', $db->post[2]->title); //Delete unset($db->post[1]); $this->assertFalse(isset($db->post[1])); - $this->assertCount(0, $db->post); + $this->assertCount(1, $db->post); } public function testSelectShortcuts()