Merging relations and organisations

Relations and Organisations can be merged together. When merging two models, the data of the model will be merged to the other model. The model will be deleted after the merge. All defined relations will be updated to the new model. This process can be customized by adding the model to the config file of the CRM package.

Configuration

In the config file (config/compass/crm.php) of the CRM package, the following array can be found:

    'mergeable_classes' => [
        \Noardcode\Compass\Crm\Models\Document::class,
        \Noardcode\Compass\Crm\Models\Relation::class,
        \Noardcode\Compass\Crm\Models\Organisation::class,
        \Noardcode\Compass\Crm\Models\Organisation\Activity::class,
        \Noardcode\Compass\Crm\Models\Organisation\Connection::class,
        \Noardcode\Compass\Crm\Models\Relation\Activity::class,
        \Noardcode\Compass\Crm\Models\Relation\Connection::class,
    ],

Additional models can be added to the array. You can overwrite this array to change the default classes. To do this you have to add the fully qualified class name of the model to the array.

To add additional classes add the following line to your ServiceProvider registries in your custom project (if not already present):

    $this->mergeConfigFrom(__DIR__.'/../../config/compass/custom-mergeable-classes-crm.php', 'compass.crm.mergeable_classes');

Make sure this custom-mergeable-classes-crm.php file exists in the config folder of your project.

Interface

After adding the model to the config, the model has to implement the following interface:

    use Noardcode\Compass\Crm\Models\Interfaces\MergeableInterface;

This interface will force the model to implement the following method:

    /**
     * Get the mergeable relations of the model.
     * @return array
     */
    public static function getMergeableColumns(): array;

The array has to make use of the MergeableColumn object. For example:

    public static function getMergeableColumns(): array
    {
        return [
            new MergeableColumn(
                'relatable', 
                OrganisationRelationInterface::class, 
                'Transport', 
                true 
            ),
        ];
    }

More than one column can be added to the array. The following parameters are available:

Parameter Description Example
name The column or relation method name defined in the model 'relatable' / 'organisation_id'
model Is the relation an organisation, relation or both OrganisationInterface::class / RelationInterface::class / OrganisationRelationInterface:class
title The title of the column. This is shown in a dynamic table to the user. 'Transport' / __('translation.Transport)
morph Is the column a morph true / false

The parameters are used to create a dynamic table in the merge view. The mergeable columns are also used to merge the data of the model to the other model.

If everything is implemented correctly, the model can be merged with another model and the defined relations will be updated to the new model.