drodsh7940 2014-08-17 07:43
浏览 30
已采纳

模块zf2中的许多表网关

I'm building an CMS with zend framework 2. I have 2 questions and hope you can help me.

  1. My module uses many different SQL tables. Following the example in skeleton-application, I have to do like this:

public function getServiceConfig() {

return array(

'factories' => array(

'Album\Model\AlbumTable' => function($sm) {}, 'AlbumTableGateway' => function ($sm) {}, 'Album\Model\Trackable' => function($sm) {}, 'TrackTableGateway' => function ($sm) {}, 'Album\Model\ArtistTable' => function($sm) {}, 'ArtistTableGateway' => function ($sm) {}, 'TrackTableGateway' => function ($sm) {}, 'Album\Model\SingerArtistTable' => function($sm) {}, 'SingerTableGateway' => function ($sm) {}, ... ), ); }

So should I put many models in gServiceConfig() like above? Or can you suggest me any other patterns?

  1. I want to ask when we use "fatories" in getServiceConfig, is it true that the model will be initialized only when we call it? Thank you so much for your reply!
  • 写回答

1条回答 默认 最新

  • doushishi2415 2014-08-17 11:11
    关注

    1) You can avoid that redundant code of creating factories for each Table Class. You'll just have something like this :

    'invokables'=>array(
    'ModuleName\Model\TableA' => 'ModuleName\Model\TableA',
    'ModuleName\Model\TableB' => 'ModuleName\Model\TableB',
    'ModuleName\Model\TableC' => 'ModuleName\Model\TableC',
     ),
    

    For that, you can follow this intersting post : Setting Default Db Adapter.

    2) Yes, The service manager doesn't make an instance of anything until you request it, i.e. the instance is created only when you call, for example :

    $this->getServiceLocator()->get('Trackable');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败