Created
August 3, 2013 16:56
-
-
Save eminetto/6147112 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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