Usage

Entity Relationship Diagram allows the creation of Entity Relationship Diagrams to describe entities, their attributes and the relationships between them.

Entities are created and attributes added to them. Attributes have a name and type and may be identified as a key. Entities are then added to the Entity Relationship Diagram.

Relationships between entities are defined and added to the Entity Relationship Diagram, which is then rendered.

Example

PHP

$customer = (new Entity('CUSTOMER'))
    ->withAttribute(
        (new Attribute('customerId', 'int'))->withKey(Key::primaryKey),
        new Attribute('name', 'string'),
        new Attribute('sector', 'string')
    )
;

$deliveryAddress = (new Entity('DELIVERY-ADDRESS'))
    ->withAttribute(
        (new Attribute('customerId', 'int'))->withKey(Key::foreignKey),
        new Attribute('streetAddress', 'string'),
        new Attribute('locality', 'string'),
        new Attribute('region', 'string'),
        new Attribute('postalCode', 'string'),
        new Attribute('country', 'string')
    )
;

$order = (new Entity('ORDER'))
    ->withAttribute(
        (new Attribute('orderNumber', 'int'))->withKey(Key::primaryKey),
        (new Attribute('customerId', 'int'))->withKey(Key::foreignKey)
    )
;

$lineItem = (new Entity('LINE-ITEM'))
    ->withAttribute(
        (new Attribute('orderNumber', 'int'))->withKey(Key::foreignKey),
        new Attribute('productCode', 'string'),
        new Attribute('quantity', 'int'),
        new Attribute('float', 'pricePerUnit')
    )
;

echo Mermaid::create(EntityRelationshipDiagram::class)
    ->withEntity($customer, $deliveryAddress, $order, $lineItem)
    ->withRelationship(
        new Relationship(
            $customer,
            Cardinality::oneOrMore,
            $deliveryAddress,
            Cardinality::oneOrMore,
            RelationshipType::nonIdentifying,
            'uses'
        ),
        new Relationship(
            $customer,
            Cardinality::exactlyOne,
            $order,
            Cardinality::zeroOrOne,
            RelationshipType::identifying,
            'places'
        ),
        new Relationship(
            $order,
            Cardinality::exactlyOne,
            $lineItem,
            Cardinality::oneOrMore,
            RelationshipType::identifying,
            'contains'
        ),
    )
    ->render()
;

Generated Mermaid

<pre class="mermaid">
erDiagram
direction TB
CUSTOMER {
  int customerId PK
  string name
  string sector
}
DELIVERY-ADDRESS {
  int customerId FK
  string streetAddress
  string locality
  string region
  string postalCode
  string country
}
ORDER {
  int orderNumber PK
  int customerId FK
}
LINE-ITEM {
  int orderNumber FK
  string productCode
  int quantity
  float pricePerUnit
}
CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
</pre>

Mermaid Diagram

        erDiagram
direction TB
CUSTOMER {
  int customerId PK
  string name
  string sector
}
DELIVERY-ADDRESS {
  int customerId FK
  string streetAddress
  string locality
  string region
  string postalCode
  string country
}
ORDER {
  int orderNumber PK
  int customerId FK
}
LINE-ITEM {
  int orderNumber FK
  string productCode
  int quantity
  float pricePerUnit
}
CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains