Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- \vedor\module\Application\Controller\LoginController.php
- */
- namespace Application\Controller;
- use Zend\Mvc\Controller\AbstractActionController;
- use Zend\View\Model\ViewModel;
- use Application\Model\SuperuserTable;
- class LoginController extends AbstractActionController
- {
- protected $superuserTable;
- public function indexAction()
- {
- print_r($this->getServiceLocator()->get('Application\Model\SuperuserTable'));
- return new ViewModel([
- 'superusers' => $this->superuserTable->fetchAll()
- ]);
- }
- public function getSuperuserTable()
- {
- if(!$this->superuserTable) {
- $sm = $this->getServiceLocator();
- $this->superuserTable = $sm->get('Application\Model\SuperuserTable');
- }
- return $this->superuserTable;
- }
- }
- ?>
- <?php
- /*
- \vendor\module\Application\src\Application\Model\Superuser.php
- */
- namespace Application\Model;
- class Superuser
- {
- public $id;
- public $name;
- public $department_id;
- public $image_profile;
- public $email;
- public $username;
- public $password;
- public function exchangeArray(array $data)
- {
- $this->id= !empty($data['id']) ? $data['id'] : null;
- $this->name= !empty($data['name']) ? $data['name'] : null;
- $this->department_id= !empty($data['department_id']) ? $data['department_id'] : null;
- $this->image_profile= !empty($data['image_profile']) ? $data['image_profile'] : null;
- $this->email= !empty($data['email']) ? $data['email'] : null;
- $this->username= !empty($data['username']) ? $data['username'] : null;
- $this->password= !empty($data['password']) ? $data['password'] : null;
- }
- }
- ?>
- <?php
- /*
- \vendor\module\Application\src\Application\Model\SuperuserTable.php
- */
- namespace Application\Model;
- use RuntimeException;
- use Zend\Db\TableGateway\TableGateway;
- class SuperuserTable
- {
- private $tableGateway;
- public function __construct(TableGatewayInterface $tableGateway)
- {
- $this->tableGateway = $tableGateway;
- }
- public function fetchAll()
- {
- return $this->tableGateway->select();
- }
- public function getSuperuser($id)
- {
- $id = (int)$id;
- $rowset = $this->tableGateway->select(['id', $id]);
- $row = $rowset->current();
- if(! $row) {
- throw new RuntimeException(sprintf(
- 'Could not find row with identifier $id',
- $id
- ));
- }
- return $row;
- }
- public function saveSuperuser(Superuser $superuser)
- {
- $data = [
- 'name' => $superuser->name,
- 'department_id' => $superuser->department_id,
- 'image_profile' => $superuser->image_profile,
- 'email' => $superuser->email,
- 'username' => $superuser->username,
- 'password' => $superuser->password,
- ];
- $id = (int) $superuser->id;
- if($id == 0) {
- $this->tableGateway->insert($data);
- return;
- }
- if(! $this->getSuperuser($id)) {
- throw new RuntimeException(sprintf(
- 'Cannot update album with identifier %d; does not exist',
- $id
- ));
- }
- $this->tableGateway->update($data, ['id'] => $id);
- }
- public function deleteSuperuser($id)
- {
- $this->tableGateway->delete(['id', (int) $id]);
- }
- }
- ?>
- <?php
- /*
- *\vendor\module\Application\Module.php
- */
- namespace Application;
- use Zend\Mvc\ModuleRouteListener;
- use Zend\Mvc\MvcEvent;
- use Application\Model\Superuser;
- use Appication\Model\SuperuserTable;
- use Zend\Db\ResultSet\ResultSet;
- use Zend\Db\TableGateway\TableGateway;
- class Module
- {
- public function onBootstrap(MvcEvent $e)
- {
- $eventManager = $e->getApplication()->getEventManager();
- $moduleRouteListener = new ModuleRouteListener();
- $moduleRouteListener->attach($eventManager);
- }
- public function getConfig()
- {
- return include __DIR__ . '/config/module.config.php';
- }
- public function getAutoloaderConfig()
- {
- return array(
- 'Zend\Loader\StandardAutoloader' => array(
- 'namespaces' => array(
- __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
- ),
- ),
- );
- }
- public function getServiceConfig()
- {
- return [
- 'factories' => [
- 'Application\Model\SuperuserTable' => function($sm) {
- $tableGateway = $sm->get('SuperuserTableGateway');
- $table = new SuperuserTable($tableGateway);
- return $table;
- },
- 'AlbumTableGateway' => function ($sm) {
- $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
- $resultSetPrototype = new ResultSet();
- $resultSetPrototype->setArrayObectPrototype(new Superuser());
- return new TableGateway('superuser', $dbAdapter, null, $resultSetPrototype);
- }
- ],
- ];
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement