ZF2 - ServiceManager introducing 84 tables ... tedious

I initially talked about this a couple of months ago regarding ZF2 injecting into tables with DI during Beta 1, and thought it was not possible. Now ZF2 was released as version 2.0.0, and the default ServiceManager is instead of DI. I think I have the same question as me, refactoring.

I have 84 tables that need a DbAdapter that inserts into them, and I'm sure there should be a better way, since I copy myself a lot.

public function getServiceConfig() { return array( 'factories' => array( 'accountTable' => function ($sm) { $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); $table = new Model\DbTable\AccountTable($dbAdapter); return $table; }, 'userTable' => function ($sm) { $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); $table = new Model\DbTable\UserTable($dbAdapter); return $table; }, // another 82 tables of the above ) ) } 

With EventsManager and ServiceManager, I have no idea where I am in getting application instances / resources.

Thanks Dom

+4
source share
3 answers

With a lot of injections like this, your best bet is to create a service activator and then implement an interface like DbAdapterAwareInterface. If you want to see the idea in action, look at the EventManagerAwareInterface in the zf2 code base.

+2
source

I wrote a ZF2 module called DiWrapper , which you can use to automatically create and automatically update this type of factory code. You will need to provide Zend\Db\Adapter\Adapter for DiWrapper as a shared instance. Then you can use ServiceManager or DiWrapper to get table classes.

0
source

this is really normal. php5.3 + introduces only * pointers or "links"

-1
source

Source: https://habr.com/ru/post/1435169/


All Articles