Last active
August 29, 2015 14:12
-
-
Save ibrahimab/e4b8b03134592224712c to your computer and use it in GitHub Desktop.
TableGateway ZF2 solution
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 | |
// Base class for all models | |
namespace Project\Model\Model; | |
use Zend\Db\TableGateway\AbstractTableGateway; | |
use Zend\Db\Adapter\Adapter; | |
use Zend\Db\ResultSet\HydratingResultSet; | |
use Zend\Stdlib\Hydrator\Reflection as ReflectionHydrator; | |
class Base extends AbstractTableGateway { | |
protected $entity; | |
public function __construct(Adapter $adapter, $entity = null) { | |
$this->adapter = $adapter; | |
$this->setEntity(($entity ?: $this->entity)); | |
$this>initialize(); | |
} | |
public function setEntity($entity = null) { | |
if (null === $entity) { | |
$reflectionClass = new \ReflectionClass($this); | |
$modelHierarchy = explode('\\', $reflectionClass->getName()); | |
$count = 0; | |
$this->entity = ''; | |
foreach ($modelHierarchy as $name) { | |
if (0 === $count) { | |
// finding Module namespace | |
$this->entity = $name . '\Model\Entity'; | |
} | |
// after the third namespace (Project\Model\Model), build class name | |
if ($count > 2) { | |
$this->entity .= '\\' . $name; | |
} | |
$count += 1; | |
} | |
} | |
if (false === class_exists($this->entity)) { | |
throw new \Exception('Entity could not be resolved (' . $this->entity . ')'); | |
} | |
$this->resultSetPrototype = new HydratingResultSet(new ReflectionHydrator, new $this->entity); | |
} | |
} | |
// users table | |
namespace Project\Model\Model; | |
class User extends Base { | |
protected $table = 'users'; | |
protected $entity = 'Project\Model\Entity\User'; // not neccessary, as Base does this for us via \ReflectionClass | |
} | |
// user entity | |
namespace Project\Model\Entity; | |
class User { | |
protected $firstname; | |
protected $lastname; | |
public function setFirstName($firstname) { | |
$this->firstname = $firstname; | |
} | |
public function getFirstName() { | |
return $this->firstname; | |
} | |
public function setLastName($lastname) { | |
$this->lastname = $lastname; | |
} | |
public function getLastName() { | |
return $this->lastname; | |
} | |
public function getFullName() { | |
return $this->getFirstName() . ' ' . $this->getLastName(); | |
} | |
} | |
// abstract factory | |
namespace Project\ServiceManager\AbstractFactory; | |
use Zend\ServiceManager\AbstractFactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class ModelAbstractFactory implements AbstractFactoryInterface { | |
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { | |
return substr($requestedName, 0, 19) === 'Project\Model\Model' && class_exists($requestedName); | |
} | |
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { | |
return new $requestedName($serviceLocator->get('Project\Db\Adapter')); | |
} | |
} | |
// how to use it: | |
$userModel = $this->getServiceLocator()->get('Project\Model\Model\User'); | |
// array of User Entities | |
$users = $userModel->select(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment