【问题标题】:How to insert data into sub-tables of a Module using SugarBean如何使用 SugarBean 将数据插入到模块的子表中
【发布时间】:2013-10-21 06:02:12
【问题描述】:

如何在模块的子表中插入数据? 比如我有这个模块:MainModule,它的表是mainmodule,模块的子表是mainmodule_sub。

所以我想知道如何使用 SugarBean 将数据插入 mainmodule_sub。

为了更清晰的视图:

问题是大部分模块只有main_tables和_cstm表,并不是所有模块都有一两个表;所以我只想知道如何将数据插入到 third_table 中,例如 ProspectLists 模块,它有这 5 个表,即前景_lists、prospect_lists_cstm、prospect_lists_prospect、prospect_lists_campaign 等等。

如何将数据插入到前景列表中?

【问题讨论】:

    标签: php sugarcrm sugarbean


    【解决方案1】:

    我相信您的意思是用于存储通过 Studio 创建的自定义字段的表,默认命名为 <module_name>_cstm。如果是这样,需要致电$bean->custom_fields->retrieve(); 才能访问。然后就可以用普通的$bean->save();来存储它的值了。

    例如:

    // Assume custom field 'account_custom_category_c' 
    $bean->custom_fields->retrieve();
    $bean->account_custom_category_c = 'Very Big Company';
    $bean->save();
    

    【讨论】:

    • 好吧,我知道如何通过 cstm 表插入它,只需使用相同的 bean。我将更新问题以便清楚,因为现在我直接使用 sql 而不是 bean 或 beanQuery。
    【解决方案2】:

    正如我从 Stack 向开发人员提出的问题一样,到目前为止,要么创建自己的 API 以完成它,要么通过 SQL 进行自己的本地调用/插入,因为只有 main_tables 和 _cstm 表是连接到每个 ModuleAPI。

    【讨论】:

      【解决方案3】:

      您可以手动将新字段添加到 _cstm 表中,因此不必冒丢失现有数据的风险。

      使用以下模板通过 SQL 和 PHP 添加字段(替换为您需要新字段的任何模块:

      ALTER TABLE <module>_cstm add COLUMN new_field_c varchar(255) NULL;
      

      末尾的_c很重要,_cstm表中的字段应该以_c结尾。

      然后,导航到

      {sugar_base_directory}/custom/Extension/modules/<module>/Ext/Vardefs
      

      在该目录中,创建一个名为 Sugarfield_new_field_c.php 的新文件

      在这个新创建的文件中,定义要添加的字段的属性(注意 $dictionary 数组中的模块名称应该是单数,例如 Prospect,而不是 Prospects):

      <?php
      $dictionary['<module_singular>']['fields']['new_field_c']['name'] = 'new_field_c';
      $dictionary['<module_singular>']['fields']['new_field_c']['vname'] = 'LBL_NEW_FIELD_C';
      $dictionary['<module_singular>']['fields']['new_field_c']['type'] = 'varchar';
      $dictionary['<module_singular>']['fields']['new_field_c']['enforced'] = '';
      $dictionary['<module_singular>']['fields']['new_field_c']['dependency'] = '';
      $dictionary['<module_singular>']['fields']['new_field_c']['required'] = false;
      $dictionary['<module_singular>']['fields']['new_field_c']['massupdate'] = '0';
      $dictionary['<module_singular>']['fields']['new_field_c']['default'] = '';
      $dictionary['<module_singular>']['fields']['new_field_c']['no_default'] = false;
      $dictionary['<module_singular>']['fields']['new_field_c']['comments'] = 'Example Vardef';
      $dictionary['<module_singular>']['fields']['new_field_c']['help'] = '';
      $dictionary['<module_singular>']['fields']['new_field_c']['importable'] = 'true';
      $dictionary['<module_singular>']['fields']['new_field_c']['duplicate_merge'] = 'disabled';
      $dictionary['<module_singular>']['fields']['new_field_c']['duplicate_merge_dom_value'] = 0;
      $dictionary['<module_singular>']['fields']['new_field_c']['audited'] = false;
      $dictionary['<module_singular>']['fields']['new_field_c']['reportable'] = true;
      $dictionary['<module_singular>']['fields']['new_field_c']['unified_search'] = false;
      $dictionary['<module_singular>']['fields']['new_field_c']['merge_filter'] = 'disabled';
      $dictionary['<module_singular>']['fields']['new_field_c']['calculated'] = false;
      $dictionary['<module_singular>']['fields']['new_field_c']['len'] = '255';
      $dictionary['<module_singular>']['fields']['new_field_c']['size'] = '20';
      $dictionary['<module_singular>']['fields']['new_field_c']['id'] = 'new_field_c';
      $dictionary['<module_singular>']['fields']['new_field_c']['custom_module'] = '<module>';
      $dictionary['<module_singular>']['fields']['new_field_c']['source'] = 'custom_fields';
      ?>
      

      然后,在表 fields_meta_data 中插入一条相应的记录,这将触发比较过程,即让 SugarCRM 知道这个新字段:

      INSERT INTO fields_meta_data (id, name, vname, comments, custom_module, type, len, required, deleted, audited, massupdate, duplicate_merge, reportable, importable) VALUES ('<module>new_field_c', 'new_field_c', 'LBL_NEW_FIELD_C', 'Example Vardef', '<module>', 'varchar', 255, 0, 0, 0, 0, 0, 1, 'true');
      

      一旦到位,进行修复和重建,您的新领域就可以使用了。

      这将使该字段与 SugarBean 兼容:

      $bean = BeanFactory::getBean('<module>');
      $bean->new_field_c = 'abcd';
      $bean->save();
      

      将识别该字段并对其进行更新。

      【讨论】:

        猜你喜欢
        • 2023-03-15
        • 2020-12-02
        • 2020-10-29
        • 1970-01-01
        • 2021-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多