Espocrm development

From bobmatnyc/claude-mpm · View interactive version

Espocrm development is an AI agent skill from the GitHub repository bobmatnyc/claude-mpm. Comprehensive guide for developing on EspoCRM - metadata-driven CRM with service layer architecture

How to use this skill

Skill file

EspoCRM Development

Overview

EspoCRM is a metadata-driven CRM platform where configuration lives in JSON files, business logic belongs in Services, and data access happens through ORM EntityManager. This skill enforces architectural patterns to prevent common mistakes like passing Container dependencies, bypassing the service layer, or implementing business logic in hooks.

When to Use This Skill

Activate when developing custom EspoCRM modules, entities, relationships, hooks, services, API endpoints, or integrations. Use especially when: working with ORM (EntityManager required), implementing business logic (belongs in Services), creating hooks (use interfaces), modifying metadata (requires cache rebuild), or building custom field types.

The Iron Law

BUSINESS LOGIC IN SERVICES, NOT HOOKS | DATA ACCESS VIA ENTITYMANAGER, NEVER DIRECT PDO | NEVER PASS CONTAINER AS DEPENDENCY

If you're accessing Container directly or writing business logic in hooks, you're violating architecture.

Core Architecture Principles

  1. Metadata-Driven: Entity definitions, layouts, field configs live in JSON
  2. Service Layer: All business logic implemented in Service classes
  3. ORM EntityManager: Central access point for all database operations
  4. Dependency Injection: Constructor injection, never pass Container
  5. Hook System: Lifecycle events for validation and side effects (not business logic)
  6. Repository Pattern: Entities accessed through repositories

Quick Start

  1. Setup Development Environment - Use ext-template, work in src/ directory (EspoCRM 7.4+), understand metadata structure: custom/Espo/Modules/{ModuleName}/Resources/metadata/
  1. Access Data with EntityManager

```php

use Espo\ORM\EntityManager;

public function __construct(private EntityManager $entityManager) {}

// Find entity

$account = $this->entityManager->getEntityById('Account', $id);

// Query with conditions

$collection = $this->entityManager

->getRDBRepository('Contact')

->where(['accountId' => $accountId])

->find();

```

  1. Implement Business Logic in Services

```php

namespace Espo\Modules\MyModule\Services;

use Espo\Services\Record;

class MyEntity extends Record {

public function customAction(string $id, object $data): object {

// Business logic here

$entity = $this->entityManager->getEntityById($this->entityType, $id);

// ... process ...

$this->entityManager->saveEntity($entity);

return $entity;

}

}

```

  1. Register Hooks for Lifecycle Events

```php

namespace Espo\Modules\MyModule\Hooks\Account;

use Espo\ORM\Entity;

use Espo\Core\Hook\Hook\BeforeSave;

class MyHook implements BeforeSave {

public function beforeSave(Entity $entity, array $options): void {

// Validation or side effects only

if ($entity->isAttributeChanged('status')) {

// React to changes

}

}

}

```

  1. Rebuild Cache After Changes

```bash

bin/command rebuild

```

Hook Types (Interfaces)

EspoCRM provides 7 hook types - ALWAYS use interfaces: BeforeSave (validation before save), AfterSave (side effects after save), BeforeRemove (validation before delete), AfterRemove (cleanup after delete), AfterRelate (relationship creation), AfterUnrelate (relationship removal), AfterMassRelate (bulk relationship operations).

Navigation

For detailed information:

Key Patterns

Correct Pattern:

✅ Service with injected dependencies
✅ EntityManager for data access
✅ Hooks using interfaces
✅ Type declarations on all methods
✅ Exceptions for error handling

Incorrect Patterns:

❌ Passing Container as dependency
❌ Direct PDO database access
❌ Business logic in hooks
❌ Hook base classes instead of interfaces
❌ Missing type declarations

Common Mistakes to Avoid

Integration with Other Skills

The Bottom Line

EspoCRM is metadata-driven with a service layer architecture.

Understand the metadata system. Use EntityManager for data. Implement business logic in Services. Use hooks for lifecycle events only. Rebuild cache after changes.

This is the EspoCRM way.

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.