Created
June 8, 2014 13:47
-
-
Save eminetto/0d5e1e5b11a53523ee36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Model; | |
use Zend\Db\TableGateway\TableGateway; | |
class PostTableGateway | |
{ | |
protected $tableGateway; | |
public function __construct(TableGateway $tableGateway) | |
{ | |
$this->tableGateway = $tableGateway; | |
} | |
public function fetchAll() | |
{ | |
$resultSet = $this->tableGateway->select(); | |
return $resultSet; | |
} | |
public function get($id) | |
{ | |
$id = (int) $id; | |
$rowset = $this->tableGateway->select(array('id' => $id)); | |
$row = $rowset->current(); | |
if (!$row) { | |
throw new \Exception("Não encontrado id $id"); | |
} | |
return $row; | |
} | |
public function save(Post $post) | |
{ | |
$data = array( | |
'title' => $post->title, | |
'description' => $post->description, | |
'post_date' => $post->post_date, | |
); | |
$id = (int) $post->id; | |
if ($id == 0) { | |
$this->tableGateway->insert($data); | |
} else { | |
if ($this->get($id)) { | |
$this->tableGateway->update($data, array('id' => $id)); | |
} else { | |
throw new \Exception('Post não existe'); | |
} | |
} | |
} | |
public function delete($id) | |
{ | |
$this->tableGateway->delete(array('id' => (int) $id)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment