Created
July 21, 2014 19:22
-
-
Save Saeven/c953630d37ed57028c85 to your computer and use it in GitHub Desktop.
PHP DynamoDB Poll using Zend Framework 2
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\Controller; | |
use Aws\DynamoDb\DynamoDbClient; | |
use Aws\DynamoDb\Enum\ComparisonOperator; | |
use Aws\DynamoDb\Enum\Type; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use Zend\View\Model\JsonModel; | |
use Zend\View\Model\ViewModel; | |
class IndexController extends AbstractActionController | |
{ | |
private $table_name = "shoppers_btr_poll"; | |
private $client; | |
public function indexAction() | |
{ | |
return new ViewModel(); | |
} | |
public function dynamoTestAction() | |
{ | |
$sm = $this->getServiceLocator(); | |
$aws = $sm->get('aws'); | |
/** @var DynamoDbClient $client */ | |
$this->client = $aws->get('dynamodb'); | |
$this->vote( 1 ); | |
$response = $this->getPollAnswers( 1, 4 ); | |
return new JsonModel( $response ); | |
} | |
private function getPollAnswers( $min, $max ) | |
{ | |
$response = $this->client->scan( array( | |
'TableName' => $this->table_name, | |
'ScanFilter' => array( | |
'poll_id' => array( | |
'ComparisonOperator' => ComparisonOperator::BETWEEN, | |
'AttributeValueList' => array( | |
array( Type::NUMBER => $min ), | |
array( Type::NUMBER => $max ), | |
), | |
), | |
), | |
)); | |
$poll = array(); | |
foreach( $response['Items'] as $x ) | |
$poll[$x['poll_id'][Type::NUMBER]] = $x['votes'][Type::NUMBER]; | |
ksort( $poll ); | |
return $poll; | |
} | |
private function vote( $poll_id ) | |
{ | |
$this->client->updateItem( array( | |
'TableName' => $this->table_name, | |
'Key' => array( | |
'poll_id' => array( Type::NUMBER => $poll_id ) | |
), | |
'AttributeUpdates' => array( | |
'votes' => array( | |
'Action' => 'ADD', | |
'Value' => array( | |
Type::NUMBER => 1 | |
), | |
), | |
), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment