drxzpo70788179614 2014-01-07 17:34
浏览 62

从zend框架2中的另一个模块获取数据

I installed SkeletonApplication and created default album module from ZF2 Docs. http://framework.zend.com/manual/2.2/en/user-guide/modules.html

After that, I created category module like album module. Here are it`s fields: category_id and category_name

Then added extra field for album table: cat_id As you understand I'm trying to categorize albums.

Then I added this code to AlbumForm.php file for fieldset choose cat_id from selectbox

$this->add(array(
    'type' => 'Zend\Form\Element\Select',
    'name' => 'cat_id',
    'options' => array(
            'value_options' => $selectOptions,
        'label' => 'Category',
     ),
)); 

And now, how can I get category_name and category_id from another category module to album module?

  • 写回答

2条回答 默认 最新

  • douquqiang1513 2014-01-07 21:29
    关注

    It would be good if we could see the code of your category module files.

    First of all, you should consider if you actually need a new module for this, since probably it should be in the same module, just creating new model classes and new views (if there is some). Anyways, at this point it doesnt matter.

    If you are using the SkeletonApplication i suppose in your Album module, in Module.php you have this code:

     public function getServiceConfig()
        {
            return array(
                    'factories' => array(
                            'Album\Model\AlbumTable' =>  function($sm) {
                                $tableGateway = $sm->get('AlbumTableGateway');
                                $table = new AlbumTable($tableGateway);
                                return $table;
                            },
                            'AlbumTableGateway' => function ($sm) {
                                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                                $resultSetPrototype = new ResultSet();
                                $resultSetPrototype->setArrayObjectPrototype(new Album());
                                return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                            },
                    ),
            );
        }
    

    And then probably you created something similar for your category tables, something like

     public function getServiceConfig()
        {
            return array(
                    'factories' => array(
                            'Category\Model\CategoryTable' =>  function($sm) {
                                $tableGateway = $sm->get('CategoryTableGateway');
                                $table = new CategoryTable($tableGateway);
                                return $table;
                            },
                            'CategoryTableGateway' => function ($sm) {
                                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                                $resultSetPrototype = new ResultSet();
                                $resultSetPrototype->setArrayObjectPrototype(new Album());
                                return new TableGateway('category', $dbAdapter, null, $resultSetPrototype);
                            },
                    ),
            );
        }
    

    Let me know if it is much diferent.

    Since all the configuration is merged, in your controller your service manager can locate also the Category\Model\CategoryTable no matter if you are in other module.

    So, you have to modify your AlbumForm class, so for instance in the constructor you can pass the dependencies, for example servicemanager, or 'Category\Model\CategoryTable' so when you need to retrieve the $selectOptions list, you can access it to make any query.

    Also, you can take a look at more fine solutions like this from samsonasik, but it can be harder to understand.

    And also, you can check this blog post and code from jamescarr to achieve more or less the same that you need. It is really easy to understand, and it is a good solution.

    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式