Fluentcrm companies model

From Lonsdale201/wp-agent-skills · View interactive version

Fluentcrm companies model is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Work with FluentCRM 3.x Companies / account records from companion plugins. Covers the experimental company_module flag, FluentCrmApi('companies'), Company model fields, createOrUpdate, owner_id as Subscriber ID, custom company fields in meta.custom_values, primary company vs many-to-many company me

How to use this skill

Skill file

FluentCRM: Companies model and contact-company relations

Use this skill when a plugin needs to sync B2B account/company records into FluentCRM or attach contacts to existing companies. Companies are core FluentCRM 3.1.13 code, but the UI/automation surface is behind the experimental company_module setting.

Guard the feature

For UI, automation actions, or user-visible sync flows, check the module flag:

use FluentCrm\App\Services\Helper;

if (!function_exists('FluentCrmApi') || !Helper::isCompanyEnabled()) {
    return;
}

Helper::isCompanyEnabled() reads _fluentcrm_experimental_settings['company_module'] === 'yes'. The low-level API does not hard-block writes when the flag is off, so do not create surprise company data from a hidden integration unless the admin opted in.

API entry points

app/Api/config.php registers the API key as companies:

$companiesApi = FluentCrmApi('companies');

Create or update a company:

$company = FluentCrmApi('companies')->createOrUpdate([
    'name'        => sanitize_text_field($accountName),
    'email'       => sanitize_email($billingEmail),
    'website'     => esc_url_raw($website),
    'type'        => 'Customer',
    'industry'    => sanitize_text_field($industry),
    'owner_id'    => (int) $fluentContactId, // Subscriber ID, not WP user ID
    'custom_values' => [
        'external_account_id' => sanitize_text_field($externalId),
    ],
]);

Important behavior from Companies::createOrUpdate():

Do not call FluentCrmApi('companies')->getCompany($name) for name lookup. The method name says $idOrName, but the 3.1.13 source checks id for numeric values and email for strings. For name lookup use:

use FluentCrm\App\Models\Company;

$company = Company::where('name', $name)->first();

Data model

Company maps fc_companies. Core fields include:

name, owner_id, industry, type, email, phone, website,
address_line_1, address_line_2, postal_code, city, state, country,
timezone, employees_number, description, logo,
linkedin_url, facebook_url, twitter_url, date_of_start, meta

Relations:

Treat company_id as the primary/display company only. A contact can belong to multiple companies through Subscriber::companies().

Attach and detach contacts

Prefer the API wrapper for bulk changes:

$result = FluentCrmApi('companies')->attachContactsByIds(
    [(int) $contactId],
    [(int) $companyId]
);

if (!$result) {
    // At least one company ID was invalid, no subscribers were found, or input was empty.
}

attachContactsByIds() validates that every requested company ID exists. It attaches all valid companies to each subscriber and sets the first company as primary only when company_id is empty.

Detach:

FluentCrmApi('companies')->detachContactsByIds([$contactId], [$companyId]);

Detach behavior:

For a single loaded contact, Subscriber::attachCompanies() and detachCompanies() are safe ORM-level helpers: they guard unsaved subscribers, cast IDs to ints, use per-row INSERT IGNORE / DELETE, refresh the relation, and only fire events for actual changes.

Hooks

Company record hooks:

Contact-company pivot hooks are legacy underscore hooks only in 3.1.13:

Do not invent slash aliases for the pivot hooks; they are not emitted in the current source.

Query and segment contacts by company

For simple reads:

use FluentCrm\App\Models\Subscriber;

$contacts = Subscriber::with(['companies'])
    ->filterByCompanies([(int) $companyId])
    ->get();

ContactsQuery / advanced filters support company-aware properties:

Keep custom controllers bounded and allowlisted. Do not query serialized Company.meta for reporting unless you accept full-table scans.

Automation company actions

Core 3.1.13 registers ApplyCompanyAction and DetachCompanyAction only when Helper::isCompanyEnabled() is true. If a companion action depends on Companies, follow the same guard and seed getBlock()['settings'] with a company field default:

'settings' => [
    'company' => null,
],

The built-in action field uses:

'type'       => 'option_selectors',
'option_key' => 'companies',

That option key is provided by OptionsController::companies() and returns [{id, title}].

Common mistakes

Cross-references

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.