Sugarcrm支持 产品指南 糖开发人员 糖开发人员指南10.0 数据框架 楷模

概述

Sugdbean类支持往返数据库的所有模型操作。任何与数据库交互的模块都使用SugarBean类,其中包含创建,读/检索,更新和删除数据库(CRUD)中的记录以及获取相关记录的方法。

Crud处理

Sugarbean处理糖数据库的最基本功能,允许您创建,检索,更新和删除数据。 

创建和检索记录

Beanfactory. 类用于Bean Loading。只要您创建或检索bean对象,应使用该类。它将自动处理bean所需的任何类。有关此信息的更多信息可以在其中找到 Beanfactory. section.

获取最近保存的bean的ID

For new records, a GUID is generated and assigned to the record id field. Saving new or existing records returns a single value to the calling routine, which is the id attribute of the saved record. The id has the following format:

aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee

您可以使用以下内容检索新创建的记录ID:

//Create bean
$bean = BeanFactory::newBean($module);

//Populate bean fields
$bean->name = 'Example Record';

//Save
$bean->save();

//Retrieve the bean id
$record_id = $bean->id;

使用特定ID保存bean

Saving a record with a specific id requires the id and new_with_id attribute of the bean to be set. When doing so, it is important that you use a globally unique identifier. Failing to do so may result in unexpected behavior in the system. An example setting an id is shown below:

//Create bean
$bean = BeanFactory::newBean($module);

//set the new flag
$bean->new_with_id = true;

//Set the record id with a static id
$id = '38c90c70-7788-13a2-668d-513e2b8df5e1';
$bean->id = $id;

//or have the system generate an id for you
//$id = Sugarcrm\Sugarcrm\Util\Uuid::uuid1()
//$bean->id = $id;

//Populate bean fields
$bean->name = 'Example Record';

//Save
$bean->save();

Setting new_with_id to true prevents the save method from creating a new id value and uses the assigned id attribute. If the id attribute is empty and the new_with_id attribute is set to true, the save will fail. If you would like for the system to generate an id for you, you can use Sugarcrm\Sugarcrm\Util\Uuid::uuid1().

区分现有记录

To identify whether or not a record is new or existing, you can check the fetched_rows property. If the $bean has a fetched_row, it was loaded from the database. An example is shown below:

if (!isset($bean->fetched_row['id'])) {
    //new record
} else {
    //existing record
}

If you are working with a logic hook such as before_save or after_save, you should check the arguments.isUpdate property to determine this as shown below:

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class logic_hooks_class
{
    function hook_method($bean, $event, $arguments)
    {
        if (isset($arguments['isUpdate']) && $arguments['isUpdate'] == false) {
            //new record
        } else {
            //existing record
        }
    }
}

?>

用独特的字段检索豆子

Sometimes developers have a need to fetch a record based on a unique field other than the id. In previous versions you were able to use the retrieve_by_string_fields() method to accomplish this, however, that has now been deprecated. To search and fetch records, you should use the 糖曲线  建造者。以下示出了这一示例:

require_once('include/SugarQuery/SugarQuery.php');
$sugarQuery = new SugarQuery();

//fetch the bean of the module to query
$bean = BeanFactory::newBean('<modules>');

//create first query
$sql = new SugarQuery();
$sql->select('id');
$sql->from($bean);
$sql->Where()->equals('<field>', '<unique value>');

$result = $sql->execute();

$count = count($result);

if ($count == 0) {
    //no results were found
} elseif ($count == 1) {
    //one result was found
    $bean = BeanFactory::getBean('<module>', $result[0]['id']);
} else {
    //multiple results were found
}

更新豆子

您可以通过获取记录来更新一个bean,然后更新属性:

//Retrieve bean
$bean = BeanFactory::retrieveBean($module, $id);

//Fields to update
$bean->name = 'Updated Name';

//Save
$bean->save();

笔记 :禁用行级安全性在访问bean时,才应将其设置为true,例如,当从自定义入口点更新引线记录时,才会设置为true。在绕过行安全时访问bean的示例是:

$bean = BeanFactory::retrieveBean($module, $record_id, array('disable_row_level_security' => true));

更新Bean而不更改修改日期

The SugarBean class contains an attribute called update_date_modified, which is set to true when the class is instantiated and means that the date_modified attribute is updated to the current database date timestamp. Setting update_date_modified to false would result in the date_modified attribute not being set with the current database date timestamp.

//Retrieve bean
$bean = BeanFactory::retrieveBean($module, $id);

//Set modified flag
$bean->update_date_modified = false;

//Fields to update
$bean->name = 'Updated Name';

//Save
$bean->save();

笔记 :禁用行级安全性在访问bean时,才应将其设置为true,例如,当从自定义入口点更新引线记录时,才会设置为true。在绕过行安全时访问bean的示例是:

$bean = BeanFactory::retrieveBean($module, $record_id, array('disable_row_level_security' => true));

删除豆子

Deleting a bean can be done by fetching it then calling the mark_deleted() method which makes sure any relationships with related records are removed as well:

//Retrieve bean
$bean = BeanFactory::retrieveBean($module, $id);

//Set deleted to true
$bean->mark_deleted($id);

//Save
$bean->save();

笔记 :禁用行级安全性在访问bean时,才应将其设置为true,例如,当从自定义入口点更新引线记录时,才会设置为true。在绕过行安全时访问bean的示例是:

$bean = BeanFactory::retrieveBean($module, $record_id, array('disable_row_level_security' => true));

夺取关系

本节介绍如何使用Sugdbean类从数据库中获取相关信息。

获取相关记录

//If relationship is loaded
if ($bean->load_relationship($link)) {
    //Fetch related beans
    $relatedBeans = $bean->$link->getBeans();
}

这是一个例子是加载与帐户相关的联系人:

//Load Account
$bean = BeanFactory::getBean('Accounts', $id);

//If relationship is loaded
if ($bean->load_relationship('contacts')) {
    //Fetch related beans
    $relatedBeans = $bean->contacts->getBeans();
}

获取 只有相关的记录ID ,使用链接加载关系:

//If relationship is loaded
if ($bean->load_relationship($link)) {
    //Fetch related record IDs
    $relatedBeans = $bean->$link->get();
}

一个示例是加载与帐户相关的联系人的记录ID:

//Load Account
$bean = BeanFactory::getBean('Accounts', $id);

//If relationship is loaded
if ($bean->load_relationship('contacts')) {
    //Fetch related beans
    $relatedBeans = $bean->contacts->get();
}

获取父记录

获取父记录类似于获取子程记录,因为您仍然需要使用链接加载关系:

//If relationship is loaded
if ($bean->load_relationship($link)) {
    //Fetch related beans
    $relatedBeans = $bean->$link->getBeans();

    $parentBean = false;
    if (!empty($relatedBeans)) {
        //order the results
        reset($relatedBeans);

        //first record in the list is the parent
        $parentBean = current($relatedBeans);
    }
}

其中一个示例是加载联系人并获取其父帐户:

//Load Contact
$bean = BeanFactory::getBean('Contacts', $id);

//If relationship is loaded
if ($bean->load_relationship('accounts')) {
    //Fetch related beans
    $relatedBeans = $bean->accounts->getBeans();

    $parentBean = false;
    if (!empty($relatedBeans)) {
        //order the results
        reset($relatedBeans);

        //first record in the list is the parent
        $parentBean = current($relatedBeans);
    }
}

话题

    本文介绍如何扩展核心糖体对象以实现自定义代码。这可用于更改库存分配通知消息或添加逻辑以更改默认模块行为。
    本文介绍了如何实现自定义Suldbean模板,然后可以通过自定义模块扩展。它还涵盖了Vardef模板,可以更具可移植和多个模块使用。默认情况下,Sugar附带了一些模板,例如公司,人员,问题和文件模板。

最后修改:2020-03-16 17:00:15