Skip to content

Commit

Permalink
Fixed the array syntax to update #41
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Apr 1, 2021
1 parent 25e9a00 commit c3ac81f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
7 changes: 5 additions & 2 deletions tests/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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()
Expand Down

0 comments on commit c3ac81f

Please sign in to comment.