Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created June 8, 2014 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eminetto/0d5e1e5b11a53523ee36 to your computer and use it in GitHub Desktop.
Save eminetto/0d5e1e5b11a53523ee36 to your computer and use it in GitHub Desktop.
<?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