Current Location: Home> Latest Articles> How to use PHP to develop custom modules for SuiteCRM

How to use PHP to develop custom modules for SuiteCRM

M66 2025-05-30

Introduction to SuiteCRM

SuiteCRM is an open source enterprise customer relationship management (CRM) system with rich functions and high customizability, suitable for small and medium-sized enterprises and large organizations. Through its flexible architecture, developers can perform secondary development and module expansion according to specific needs.

Preparations before development

Before module development, please make sure that you have built a SuiteCRM environment and have corresponding system access and development permissions. At the same time, basic PHP programming knowledge is required to understand and apply relevant code examples.

Create a custom module

To create a custom module, you need to create a new module directory under the custom/modules directory. For example, we could create a folder named CustomModule as the root directory of the module.

In the CustomModule folder, the following structure is usually included:

  • CustomModule.php : The core class of the module, defining the basic behavior.
  • language folder: language package file, support localization.
  • metadata folder: contains information such as field definition, database structure, etc.

Define module class

In CustomModule.php , we define the main class of the module and inherit SugarBean class provided by SuiteCRM. The sample code is as follows:

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

class CustomModule extends SugarBean
{
    // Module name
    public $module_name = 'CustomModule';

    // Data table name
    public $table_name = 'custom_module';

    // Module object alias
    public $object_name = 'CustomModule';

    // Primary Key Field
    public $object_field = 'id';

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Implement interface judgment
     */
    public function bean_implements($interface)
    {
        switch ($interface) {
            case 'ACL': return true;
        }
        return false;
    }
}

Define the field structure

The field definition of the module is usually placed in the metadata folder, such as creating a custom_module.php file, with the following content:

 
<?php
$dictionary['CustomModule'] = array(
    'table' => 'custom_module',
    'fields' => array(
        array(
            'name' => 'id',
            'type' => 'id',
            'required' => true,
            'len' => 36,
        ),
        array(
            'name' => 'name',
            'type' => 'varchar',
            'len' => 255,
            'required' => true,
        ),
        // More field definitions can be added
    ),
    'indices' => array(
        array(
            'name' => 'custom_module_pk',
            'type' => 'primary',
            'fields' => array('id'),
        ),
    ),
);

Perform database operations

SuiteCRM provides rich classes and methods to operate databases, such as adding, deleting, modifying and checking using SugarQuery and BeanFactory . The following are common operations examples:

Query data

 
$query = new SugarQuery();
$query->from(BeanFactory::getBean('CustomModule'));
$query->select(array('id', 'name'));
$query->where()->equals('name', 'John Doe');
$result = $query->execute();

Create data

 
$customModule = BeanFactory::newBean('CustomModule');
$customModule->name = 'John Doe';
$customModule->save();

Update data

 
$customModule = BeanFactory::getBean('CustomModule', '12345678-1234-1234-1234-1234567890ab');
$customModule->name = 'Jane Doe';
$customModule->save();

Delete data

 
$customModule = BeanFactory::getBean('CustomModule', '12345678-1234-1234-1234-1234567890ab');
$customModule->mark_deleted('12345678-1234-1234-1234-1234567890ab');

Summarize

This article introduces the key steps in developing custom modules in SuiteCRM using PHP, including module creation, class definition, field structure setting, and database operation methods. By mastering these basic processes, developers can flexibly expand CRM functions according to actual needs and build a management system that complies with business processes.