SuiteCRM 二次开发:关于 bean 的数据操作,及关联关系数据读取

更新日期: 2024-03-22 阅读次数: 178 字数: 287 分类: 管理

bean 是什么

想起了 java bean。。。SuiteCRM 中 bean 的含义:

Beans are the Model in SuiteCRM’s MVC (Model View Controller) architecture.

实际上理解为 Model 即可。类似 PHP Laravel MVC 框架中的 Model,数据模型。

基础操作

if (empty($bean->first_name)) {
    $bean->first_name = "bot";
    // $bean->save();  // 注意这里不需要主动调用 save,如果是 logic hook 对应的原始逻辑中已经有了保持的逻辑的话,这样会造成重复写入时的唯一键冲突报错
}

拉取关联的记录列表

例如,拉取客户 (Account) 关联的所有联系人 (Contact):

// Load the relationship
$bean->load_relationship('contacts');

// Can now call methods on the relationship object:
// 获取 id 列表
$contacts = $bean->contacts->get();

foreach ($contacts as $id) {
        $GLOBALS['log']->fatal('id: ' . $id);
        // id: 5922ba52-c7b8-2573-d782-65fcfbe88e48
}

// 获取对象列表
$items = $bean->contacts->getBeans();
foreach ($items as $item) {
        $GLOBALS['log']->fatal('last name: ' . $item->last_name);
}

如何确定 relationship 的名称

https://docs.suitecrm.com/developer/working-with-beans/

Before accessing a relationship you must use the load_relationship call to ensure it is available. This call takes the link name of the relationship (not the name of the relationship). As mentioned previously you can find the name of the link in cache/modules//Vardefs.php if you’re not sure.

例如:

cache/modules/Accounts/Accountvardefs.php

'contacts' =>
    array (
      'name' => 'contacts',
      'type' => 'link',
      'relationship' => 'accounts_contacts',
      'module' => 'Contacts',
      'bean_name' => 'Contact',
      'source' => 'non-db',
      'vname' => 'LBL_CONTACTS',
    ),

tags: CRM

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式