AbstractFactories to create TableGateways
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 | |
return [ | |
'service_manager' => [ | |
'abstract_factories' => [ | |
'Application\Service\Factory\TableGatewayAbstractFactory', | |
'Application\Service\Factory\TableAbstractFactory', | |
] | |
] | |
] |
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\Service\Factory; | |
use Zend\ServiceManager\AbstractFactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class TableGatewayAbstractFactory implements AbstractFactoryInterface | |
{ | |
/** | |
* Determine if we can create a service with name | |
* | |
* @param ServiceLocatorInterface $serviceLocator | |
* @param $name | |
* @param $requestedName | |
* @return bool | |
*/ | |
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) | |
{ | |
return strpos($name, 'Album\Model\\') === 0; | |
} | |
/** | |
* Create service with name | |
* | |
* @param ServiceLocatorInterface $serviceLocator | |
* @param $name | |
* @param $requestedName | |
* @return mixed | |
*/ | |
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) | |
{ | |
$className = substr($name, strlen('Album\Model\\')); | |
$entityName = substr($className, 0, strpos($className, 'Table')); | |
$tableGateway = $serviceLocator->get('TableGateway\\' . $entityName); | |
return new $className($tableGateway); | |
} | |
} |
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\Service\Factory; | |
use Zend\ServiceManager\AbstractFactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
use Zend\Db\ResultSet\ResultSet; | |
use Application\Entity\User; | |
use Zend\Db\TableGateway\TableGateway; | |
class TableGatewayAbstractFactory implements AbstractFactoryInterface | |
{ | |
/** | |
* Determine if we can create a service with name | |
* | |
* @param ServiceLocatorInterface $serviceLocator | |
* @param $name | |
* @param $requestedName | |
* @return bool | |
*/ | |
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) | |
{ | |
$array = explode('\\', $name); | |
return count($array) === 2 && $array[0] === 'TableGateway'; | |
} | |
/** | |
* Create service with name | |
* | |
* @param ServiceLocatorInterface $serviceLocator | |
* @param $name | |
* @param $requestedName | |
* @return mixed | |
*/ | |
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) | |
{ | |
list($tableGateway, $entityName) = explode('/', $name); | |
$tableName = strtotolower($entityName); | |
$entityName = '\Application\Entity\\' . $entityName; | |
$dbAdapter = $serviceLocator->get('Zend\Db\Adapter\Adapter'); | |
$resultSetPrototype = new ResultSet(); | |
$resultSetPrototype->setArrayObjectPrototype(new $entityName()); | |
return new TableGateway($tableName, $dbAdapter, null, $resultSetPrototype); | |
} | |
} |
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 | |
$userTableGateway = $sm->get('TableGateway\User'); | |
$albumTableGateway = $sm->get('TableGateway\Album'); | |
$somethingTableGateway = $sm->get('TableGateway\Something'); | |
// ... | |
$userTable = $sm->get('Album\Model\UserTable'); | |
$albumTable = $sm->get('Album\Model\AlbumTable'); | |
$somethingTable = $sm->get('Album\Model\SomethingTable'); | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment