Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Feb 26, 2016
1 parent 883d2e1 commit 18bc1ef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
9 changes: 3 additions & 6 deletions src/Queries/Mysql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Select extends Query
{
const MODE_ONE = 1;
const MODE_ALL = 2;
const MODE_ARRAY = 3;

use ExtendedSelectionTrait;

Expand All @@ -40,13 +39,11 @@ public function one()
/**
* Change the mode to returns all rows.
*
* @param bool $asArray
*
* @return self
*/
public function all($asArray = false)
public function all()
{
$this->mode = $asArray ? self::MODE_ARRAY : self::MODE_ALL;
$this->mode = self::MODE_ALL;

return $this;
}
Expand All @@ -64,7 +61,7 @@ public function run()
return ($row = $statement->fetch()) === false ? null : $this->createRow($row);
}

$result = $this->mode === self::MODE_ALL ? $this->table->createCollection() : [];
$result = $this->table->createCollection();

while (($row = $statement->fetch())) {
$result[] = $this->createRow($row);
Expand Down
29 changes: 14 additions & 15 deletions src/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function __get($name)
return ($this->relations[$name] = call_user_func([$this, $name])->run()) ?: new NullValue();
}

//Exists as a function
if (method_exists($this, $name)) {
return $this->$name();
}
Expand Down Expand Up @@ -188,20 +189,20 @@ public function save($relations = false)
}
}

if (!$this->changed) {
return $this;
}
if ($this->changed) {
if (empty($this->id)) {
$this->id = $this->table->insert()
->data($this->values)
->run();
} else {
$this->table->update()
->data($this->values)
->byId($this->id)
->limit(1)
->run();
}

if (empty($this->id)) {
$this->id = $this->table->insert()
->data($this->values)
->run();
} else {
$this->table->update()
->data($this->values)
->byId($this->id)
->limit(1)
->run();
$this->table->cache($this);
}

if ($relations) {
Expand Down Expand Up @@ -248,8 +249,6 @@ public function save($relations = false)
}
}

$this->table->cache($this);

return $this;
}
}
3 changes: 2 additions & 1 deletion tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public function testManyToManyData()
$selected = $db->category->select()->run();

$this->assertEquals((string) $selected, (string) $post->category);
$this->assertEquals((string) $selected, (string) $db->post->select()->run()->category);
}

public function testLeftJoin()
Expand All @@ -153,7 +154,7 @@ public function testLeftJoin()

$this->assertCount(2, $comments);

$json = '[{"id":1,"text":"Hello world","post_id":1,"post":{"id":1,"title":"first","comment":null}},{"id":2,"text":"Hello world 2","post_id":null}]';
$json = '[{"id":1,"text":"Hello world","post_id":1,"post":{"id":1,"title":"first","comment":null,"category":[]}},{"id":2,"text":"Hello world 2","post_id":null}]';

$this->assertEquals($json, (string) $comments);
}
Expand Down

0 comments on commit 18bc1ef

Please sign in to comment.