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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页