Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created August 3, 2013 16:56
Show Gist options
  • Select an option

  • Save eminetto/6147112 to your computer and use it in GitHub Desktop.

Select an option

Save eminetto/6147112 to your computer and use it in GitHub Desktop.
<?php
namespace Aluno\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
class AlunoTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getAluno($matricula)
{
$matricula = (int) $matricula;
$rowset = $this->tableGateway->select(array('matricula' => $matricula));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Não encontrou matrícula $matricula");
}
return $row;
}
public function saveAluno(Aluno $aluno)
{
$data = array(
'nome' => $aluno->nome,
);
$matricula = $aluno->matricula;
if ($matricula == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getAluno($matricula)) {
$this->tableGateway->update($data, array('matricula' => $matricula));
} else {
throw new \Exception('A matrícula do aluno não existe');
}
}
}
public function deleteAluno($matricula)
{
$this->tableGateway->delete(array('matricula' => $matricula));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment