SugarCRM 一对多 DetailView页面显示

需求:DetailView页面中显示与父表相关的子表ListView并可增加子表内容
    解决方案:
        1.两个模块
            bw_Consignments  提运单信息     父表
            bw_ConsignItems  提运单明细     子表    关系: 子表中ConsignmentID字段为父表中的id
        2.关系字段
            ConsignmentID 必须为主表字段
            如何创建主表字段 详见 主表-自定义字段
        3.增加关系
            \modules\bw_Consignments\vardefs.php    父表模块下 添加如下

             //BUILDER: included fields
			    'bw_ConsignItems' =>          //子表模块名
			    array (
				    'name' => 'bw_ConsignItems',    //子表模块名
				    'type' => 'link',
				    'relationship' => 'MR_bw_ConsignItems',   //关系名
				    'source'=>'non-db',
				    'vname'=>'LBL_REVMSGS',
				    ), 
            //BUILDER:END of fields    
        
            //Relationships one-to-many               //注意位置不能错,否则生成的sql语句有问
            'relationships' => array(
			    'MR_bw_ConsignItems' => array(
				    'lhs_module'=> 'bw_ConsignItems', 'lhs_table'=> 'bw_ConsignItems', 'lhs_key' => 'ConsignmentID', //子表模块
				    'rhs_module'=> 'bw_Consignments', 'rhs_table'=> 'bw_Consignments', 'rhs_key' => 'id',            //父表模块
				    'relationship_type'=>'one-to-many'),//关系
		4.设置subpanel的显示		
            \modules\bw_Consignments\layout_defs.php   父表模块下 添加如下

            $layout_defs['bw_Consignments'] = array(
                 'subpanel_setup'   => array(
                    //BUILDER:END of subpanels

	                'bw_consignitems' => array(                     //必须小写
		                'order' => 25,    
		                'module' => 'bw_ConsignItems',              //子表模块名
		                'subpanel_name' => 'default',   
		                'get_subpanel_data' => 'bw_ConsignItems',   //子表模块名
		                'add_subpanel_data' => 'bw_Consignments',   //父表模块名
		                'title_key' => 'LBL_MODULE_NAME',           //subpanel的标题
		                'top_buttons' => array(	array('widget_class' => 'SubPanelTopCreateButton')			
			            ),
		             ),
                    
        		    'ser_rights' => array(
				        'order' => 26,                                  //布局  多个SubPanel时显示的顺序
				        'module' => 'ser_Rights',
				        'subpanel_name' => 'default',
				        'get_subpanel_data' => 'ser_Rights',
				        'add_subpanel_data' => 'ser_right_id',
				        'title_key' => 'LBL_SER_RIGHTS_SUBPANEL_TITLE',//subpanel的标题 在父模块的 \language\zh_cn.lang.php 增加 'LBL_SER_RIGHTS_SUBPANEL_TITLE'=> '维权服务',
				        'top_buttons' => array(   //array('widget_class' => 'SubPanelTopCreateButton')		//新增 按钮	注掉则不显示按钮
					   ),
				     ),
 
                 ),
              );
        5.DetailView中显示
            \modules\bw_Consignments\DetailView.php
            ///////////////////////////////////////////////////////////////////////////////
            ////	SUBPANELS
            ///////////////////////////////////////////////////////////////////////////////
            
              require_once('include/SubPanel/SubPanelTiles.php');
              $subpanel = new SubPanelTiles($focus, 'MRConsignments');
              echo $subpanel->display();
            
            去掉上面注释的代码,即可在DetailView视图中显示subpanel
        6.子表保存时,将赋父表的id
             \modules\bw_ConsignItems\Save.php    //在子表模块中
             在下面代码后
             $sugarbean = new bw_ConsignItems();
             $sugarbean = populateFromPost('', $sugarbean);
             $sugarbean->bw_Consignment_id = $_POST['return_id']; 
             添写
             if ($_REQUEST['return_module']=='bw_Consignment'){
                $sugarbean->ConsignmentID = $_REQUEST['return_id'];  //将父表id 赋给子表的关系字段 ConsignmentID
             }
        7.刷新关系(非常重要,不刷新则子表全部显示)
            系统管理 -> 升级 -> 重建关系(重建meta数据关系,删除cache文件)
 
        8.此步骤不写好像也行
            \modules\bw_ConsignItems\field_arrays.php
            $fields_array['bw_ConsignItem'] = array (
            'column_fields' => array(
              'id',
              'date_entered',
              'date_modified',
              'assigned_user_id',
              'modified_user_id',
              'created_by',
              'name',
              'description',
              'deleted',
            //BUILDER: included fields
              'ConsignmentID',              //关系字段  不写好像也行
            //BUILDER:END of column fields
 
        9.subpanel显示列
            在子表模块的subpanel文件夹下的 Default.php文件中
            \modules\bw_ConsignItems\default.php
	            'list_fields' => array(
		            'name'         =>array(
	 	            'vname'         => 'LBL_LIST_NAME',
			            'widget_class'  => 'SubPanelDetailViewLink',
	 	            'width'         => '20%',
		            ),
		            //自定义列
		            'SequenceNumeric'         =>array(
	 	            'vname'         => 'LBL_SequenceNumeric',
			            'widget_class'  => 'SubPanelDetailViewLink',
	 	            'width'         => '20%',
		            ),
		            'MarksNumbers'         =>array(
	 	            'vname'         => 'LBL_MarksNumbers',
			            'widget_class'  => 'SubPanelDetailViewLink',
	 	            'width'         => '20%',
		            ),
		            //下面是修改,删除 按钮可删除则不显示(不删除,按钮也不好用) 
		            'edit_button'  =>array(
			            'widget_class'  => 'SubPanelEditButton',
			            'module'        => 'bw_ConsignItems',
	 	            'width'         => '4%',
		            ),
		            'remove_button'=>array(
			            'widget_class'  => 'SubPanelRemoveButton',
			            'module'        => 'bw_ConsignItems',
			            'width'         => '5%',
		            ),
	            ),
            );
posted @ 2011-02-13 17:49  海乐学习  阅读(1076)  评论(0编辑  收藏  举报