doudao9915 2011-03-09 16:01
浏览 28
已采纳

递归函数逻辑问题

I have a recursive function with a bug that I am having trouble with. The logic is as follows: Forms have Fields of various types. One type of field is a Reference Field, which links a Form to another Form. And, References can chain. Example: I can have a Prescription Form with a Reference to an Encounter, that has a Reference to Doctor that has a Reference to Person. The same Encounter Form has a Reference to Patient That also refers to Person.

So, the Reference field chain for Doctor Last Name would look like Field 49 on Prescription refers to Encounter. Field 7 on Encounter refers to Doctor. Field 6 on Doctor refers to Person. Field 1 on Person refers to Last Name. [49-7-6-1]

The Reference chain for Patient Last Name would be [49-5-4-1] following the same logic.

My problem is this. With the current function, the Reference chain strings have problems if I have branching references at a lower level. Instead of ending up with [49-5-4-1] for Patient Last Name, I get [49-7-5-4-1].

The function is as follows:

function get_reference_fields($form_id, $base_fields = array(), $ref_field_id = null, $ref_string = null, $field_name = null,$base_form = null,$form_name=null) {

         if(!$base_form) {
             $base_form = $form_id;
         }
         App::import('model', 'Section');
         $zmr_sections = new Section();


         $sectionList = $zmr_sections->getSectionList($form_id);


         if ($sectionList) {

             $new_base_fields = $this->Field->find('list', array('joins' => array(
                 array(
                     'table' => 'field_types',
                     'alias' => 'Type',
                     'type' => 'left',
                     'foreignKey' => false,
                     'conditions' => array('Type.id = Field.field_type_id')
                 )), 'conditions' => array('Type.base_type != "formatting" AND Type.base_type != "reference" AND Type.base_type != "reverse reference"', 'Field.active=1', 'Field.section_id IN (' . $sectionList . ')')));


             if ($ref_field_id) {
                 $newFields = array();


                 foreach ($new_base_fields AS $key => $field) {
                     if ($field_name) {

                         $field = $form_name . ' - ' . $field;
                     }

                     $newFields[$ref_string . '-' . $key] = $field;

                 }
                 $new_base_fields = $newFields;
             }
             if (!$base_fields) {
                 $base_fields = array();
             }

             $base_fields = $base_fields + $new_base_fields;


             $reference_fields = $this->Field->find('all', array('joins' => array(
                 array(
                     'table' => 'field_types',
                     'alias' => 'Type',
                     'type' => 'left',
                     'foreignKey' => false,
                     'conditions' => array('Type.id = Field.field_type_id')
                 )), 'conditions' => array('Type.base_type' => 'reference', 'Field.active=1', 'Field.section_id IN (' . $sectionList . ')')));


             foreach ($reference_fields AS $reference_field) {

                 $field_name = $reference_field['Field']['name'];
                 $query = "SELECT form_id FROM zmr_lists WHERE id=" . $reference_field['Field']['zmr_list_id'];

                 $list_data = $this->query($query);
                 $new_form_id = $list_data[0]['zmr_lists']['form_id'];



                 if($form_id == $base_form){
                     $ref_string = $reference_field['Field']['id'];
                     $form_name = $this->Field->Section->Form->field('Form.label',array('Form.id'=>$new_form_id));

                 }else{
                     $ref_string .= '-'. $reference_field['Field']['id'];
                     $form_name .= '-'. $this->Field->Section->Form->field('Form.label',array('Form.id'=>$new_form_id));

                 }
                 $base_fields = $this->get_reference_fields($new_form_id, $base_fields, $reference_field['Field']['id'], $ref_string, $field_name,$base_form,$form_name);

             }


         }

         return $base_fields;

     }

==================================
And, a sample result set for the data is:

[49-7-6-48] => Doctor - Primary Role
[49-7-6-43] => Doctor - State
[49-7-6-11] => Doctor - Password
[49-7-6-2] => Doctor - Last Name
[49-7-6-10] => Doctor - Username
[49-7-6-28] => Doctor - City
[49-7-6-1] => Doctor - First Name
[49-7-6-24] => Doctor - Middle Initial
[49-7-6-27] => Doctor - Address2
[49-7-6-25] => Doctor - Date of Birth
[49-7-6-47] => Doctor - Phone Number
[49-7-6-26] => Doctor - Address1

[49-7-5-68] => PSG Study - Patient ID Number
[49-7-5-67] => PSG Study - Insurance Provider's Name
[49-7-5-74] => PSG Study - PCP referral required?
[49-7-5-73] => PSG Study - Pre-Authorization Obtained?
[49-7-5-22] => PSG Study - Special Needs
[49-7-5-76] => PSG Study - PCP Phone Number
[49-7-5-69] => PSG Study - Group Number
[49-7-5-70] => PSG Study - Relationship to Insured Member
[49-7-5-71] => PSG Study - Referring Doctor
[49-7-5-75] => PSG Study - If Yes, PCP Name:
[49-7-5-72] => PSG Study - Diagnoses:
[49-7-5-4-48] => Patient - Primary Role
[49-7-5-4-43] => Patient - State
[49-7-5-4-11] => Patient - Password
[49-7-5-4-2] => Patient - Last Name
[49-7-5-4-10] => Patient - Username
[49-7-5-4-28] => Patient - City
[49-7-5-4-1] => Patient - First Name
[49-7-5-4-24] => Patient - Middle Initial
[49-7-5-4-27] => Patient - Address2
[49-7-5-4-25] => Patient - Date of Birth
[49-7-5-4-47] => Patient - Phone Number
[49-7-5-4-26] => Patient - Address1

=========================

Can anybody help me figure out an algorithm to properly generate the reference chains? Please note, it is technically possible for a form to refer to itself. For example, a Doctor can have a Reference to another Doctor (trainee or supervisor, etc.)

  • 写回答

1条回答 默认 最新

  • dqkkrt8860 2011-03-09 21:01
    关注

    I found the answer. I knew the initialization of the ref_string was the problem. I just couldn't get my head around it. I added this immediately after the recursive function call

      if ($ref_string) {
                            $ref_array = explode('-', $ref_string);
                            array_pop($ref_array);
                            $ref_string = implode('-', $ref_array);
                            $form_name_array = explode('-', $form_name);
                            array_pop($form_name_array);
                            $form_name = implode('-', $form_name_array);
                        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用