Reputation: 279
I am setting up my tables in module.php using the default example in zend2 tutorial, but in my project I have so much tables, so my module.php is too large.
Here is my default config EXAMPLE 1:
'UsersTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Users());
return new TableGateway('users', $dbAdapter, null, $resultSetPrototype);
},
'Application\Model\UsersTable'=>function ($sm){
$tableGateway = $sm->get('UsersTableGateway');
return new UsersTable($tableGateway);
},
my question is, if I put the UserTableGateway config into Application\Model\UserTable like this EXAMPLE 2:
'Application\Model\UsersTable'=>function ($sm){
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Users());
$tableGateway = new TableGateway('users', $dbAdapter, null, $resultSetPrototype);
return new UsersTable($tableGateway);
},
This method works for me here, nothing changes on my project, dont display any errors and the project keep working fine.
So, Way in the tutorial is saying to set up the UserTableGateway on a separete array?
And if I alter the default config(example 1 above) setting up all in Application\Model\Table like (example 2 above), is a good way to config the tablesGateway? is a good pratice?
thanks.
Upvotes: 1
Views: 194
Reputation: 5738
In short what you've done is fine but I would argue that it isn't best practice.
Configuring services in module.php
isn't the best habit to get into, it becomes very messy very quickly as you've discovered. A better direction is to use more features of ZF2 to help your predicament.
Let's move away from closures. If your models require other dependencies it's best to create factories
and point your Application\Model\UsersTable
to a factory class rather than a closure. For example, in your module.config.php
:
'service_manager' => array(
'factories' => array(
'Application\Model\UsersTable' => 'Application\Model\Factory\UsersTableFactory'
)
)
Where Application\Model\Factory\UsersTableFactory
looks roughly like this:
namespace Application\Model\Factory;
class UsersTableFactory
{
public function __invoke(ServiceLocatorInterface $sl)
{
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Users());
$tableGateway = new TableGateway('users', $dbAdapter, null, $resultSetPrototype);
return new UsersTable($tableGateway);
}
}
This can be repeated for all your models plus any other services you may have.
Something to consider
You mention you have a lot of tables and I'm guessing a lot of models. This would mean a lot of factories with a lot of duplicate code, yuk.
This is where we can use abstract factories. Assuming the construction of your models are very similar we can potentially have just one factory that can create all your models.
I'm not going to write an example of these because they can get complicated and will be better if you investigate yourself. In short an abstract factory
has two jobs: checking it can create a service, and actually creating it.
Upvotes: 3