Skip to content

Instantly share code, notes, and snippets.

@caleywoods
Forked from jamescarr/Poll.php
Created September 1, 2012 01:49
Show Gist options
  • Save caleywoods/3562573 to your computer and use it in GitHub Desktop.
Save caleywoods/3562573 to your computer and use it in GitHub Desktop.
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
<?php
namespace Poll\Model;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Poll {
public $id,
$question,
$pubDate;
public function exchangeArray( $data ){
$this->id = ( isset($data['id']) ) ? $data['id'] : null;
$this->question.g = ( isset($data['artist']) ) ? $data['artist'] : null;
$this->pubDate = ( isset($data['pubDate']) ) ? $data['pubDate'] : null;
}
public function getArrayCopy(){
return get_object_vars( $this );
}
public function setInputFilter( InputFilterInterface $inputFilter ){
throw new \Exception( 'Not used' );
}
public function getInputFilter(){
if ( !$this->inputFilter ) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'question',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
<?php
// module/Poll/src/Poll/Model/PollTable.php:
namespace Poll\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
class PollTable extends AbstractTableGateway {
protected $table = 'album';
public function __construct( Adapter $adapter ){
$this->adapter = $adapter;
$this->resultSetPrototype = new ResultSet();
$this->resultSetPrototype->setArrayObjectPrototype( new Poll() );
$this->initialize();
}
public function fetchAll(){
// Could return $resultSet = $this->select();
$resultSet = $this->select();
return $resultSet;
}
public function getPoll( $id ){
$id = (int) $id;
$rowset = $this->select( array('id' => $id) );
$row = $rowset->current();
if ( !$row ) {
throw new \Exception( 'Could not find row $id');
}
return $row;
}
public function savePoll( Poll $album ){
$data = array(
'pubDate' => $album->pubDate,
'question' => $album->question,
);
$id = (int)$album->id;
if ( $id == 0 ) {
$this->insert( $data );
} else {
if ( $this->getPoll($id) ) {
$this->update( $data, array('id' => $id) );
} else {
throw new \Exception( 'Form id does not exist' );
}
}
}
public function deletePoll( $id ){
$this->delete( array('id' => $id) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment