On 15 March 2013 19:18, Brad Waite <[email protected]> wrote:
> Interesting approach, Robert.
>
> Where DO you call the dbAdapter service, then? If I'm following, you'll need 
> to call it at least once in your controller
> or models wherein you need db access.

Well, the flow is like this:

The controller is created via a factory, like this:
https://github.com/robertbasic/perfi/blob/master/module/HexUser/src/HexUser/Factory/Controller/Console.php

Notice that it calls hexUserServiceUser (that's, my service layer)

That service is again created via a service, like here
https://github.com/robertbasic/perfi/blob/master/module/HexUser/config/module.config.php#L26

That factory then calls hexUserModelTableUser, which is then created
via an Abstract Factory which when finally creating my Table object,
calls the dbAdapter

https://github.com/robertbasic/perfi/blob/master/module/HexCommon/src/HexCommon/Factory/AbstractTableAbstractFactory.php#L40

It might seem a bit convoluted, javalike, or whatever, but I find this
approach really good to work with, very easy to separate out things
and of course, very easy to test every part

The only reason I set a static adapter is really to easily get the
adapter for the Db\NoRecordExist validator for my model here
https://github.com/robertbasic/perfi/blob/master/module/HexUser/src/HexUser/Model/User.php#L65
but of course I could have solved this in several different ways -
make the model service locator aware and then grab from the SL the db
adapter, or do the whole factory dance for the model as well, but I
really was just curious how to set a static db adapter, and did it.


>
> For what it's worth, I added the following to 
> Application\Module\Module->onBootstrap():
>
>         // set static database adapter
>         $sm = $e->getApplication()->getServiceManager();
>         $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
>         Feature\GlobalAdapterFeature::setStaticAdapter($dbAdapter);
>
> Seems to work like a champ. The only warning in the docs, from what I could 
> find, is that onBootstrap() is called for
> every request, so if I don't need to hit the database, the adapter is still 
> created. As you said, I can live with this,
> especially since very few pages will NOT need the db.
>
> BTW, your code also helped me connect some now-really-obvious dots. Namely, 
> the factories array returned in
> Module->getServiceConfig() can be placed in module.config.php with the 
> 'service_manager' key. I don't know if it's
> necessarily any better or worse to do so, but it's useful to know.
>
> -Brad
>
> On 3/15/2013 11:33 AM, Robert Basic wrote:
>
>> I've done it like this 
>> https://github.com/robertbasic/perfi/blob/master/config/autoload/global.php
>>
>> now, the only problem I see with this is if I never call the dbAdapter 
>> service from the SM in the request, but I do need
>> the static adapter, it won't be set, because the dbAdapter factory was never 
>> called. But I still have to come to a
>> situation like this, so i can live with this.



-- 
~Robert Basic;
http://robertbasic.com/

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to