stave.dev

Storage Model

Two MySQL tables hold every entity type you define. A per-type pivot view gives normal SQL its row-per-record shape back.

The Two Tables

The Entity Framework writes into just two tables, however many types you define. core/sql/core.sql creates both, along with the event type and event_view:

entity_type

id (INT, auto-increment) label (VARCHAR(255), unique)

One row per type. label is the class name folded to lower case (Note → note).

entity

type (INT) id (INT) attr (INT) value (VARCHAR(255), nullable)

One row per attribute value. Unique key on (type, id, attr). MyISAM, partitioned by LIST (type).

{label}_view

id, then one column per attribute

A CREATE VIEW built by Entity::updateView() that pivots the EAV rows with MAX(CASE WHEN attr = N THEN value END). This is what your SELECT statements should read.

Schema Rows vs. Record Rows

The entity table uses id to carry two kinds of row:

Row Type Where id is attr means value means
Schema id = 0 0-based attribute index Attribute name (title, created_at…)
Record id > 0 Attribute index (matches the schema) The value, a string with no type conversion

A type's schema is the set of its rows with id = 0. Adding an attribute is one more insert. A new record's id is max(id) + 1 within its type (Entity::getNextId()).

From EAV Rows to a Pivoted View

Suppose note is the third type registered, and you call:

new \Zero\Entity\Note(['title' => 'First',  'body' => 'Hello']);
new \Zero\Entity\Note(['title' => 'Second', 'body' => 'More']);

The rows look like this:

entity (id = 0, schema)

typeidattrvalue
300title
301body

entity (id > 0, records)

typeidattrvalue
310First
311Hello
320Second
321More
→

note_view (generated)

idtitlebody
1FirstHello
2SecondMore

The view is an ordinary view, so filters, ordering, joins and aggregates work on it. Every column is a string:

SELECT * FROM note_view WHERE title = 'First';
SELECT COUNT(*) FROM note_view;
SELECT event, COUNT(*) AS times
  FROM event_view
  GROUP BY event
  ORDER BY times DESC;

The constructor rebuilds the view when it registers a type or appends an attribute. An attribute first created by assigning a property on an existing record is stored, but the view has no column for it until forceUpdateView() is called. See Querying.

Query the view. Write through the class.

Rule of thumb: read from {label}_view (joins, pagination, aggregation are easy), write through the Entity class (the schema rows, the partition and the view are handled for you).

Partitioning

The entity table is partitioned by LIST (type). The first constructor call for a new type inserts its entity_type row and adds that id's partition, named e_type{id} (core/entity/Entity.php):

ALTER TABLE entity ADD PARTITION (PARTITION e_type3 VALUES IN (3));

core.sql creates the table with the single partition e_type2 for the event type, which takes id 2 on an empty database. Defining a type therefore runs an ALTER TABLE, so the database user needs that privilege.

The Seeded event Type

core.sql seeds one type: event, with the schema rows datetime, event, detail, ip in that order, and event_view over it. Zero\Entity\Event finds the type by its label. It is core's event log, written by the #[AuditLog] attribute (core/attribute/AuditLog.php), by core/lib/Mail/Mail.php and by the Auth module. See Examples.

Connecting

There is nothing to include and nothing to initialise. Entity classes autoload, and the last line of core/entity/Entity.php connects when the file is loaded:

Entity::init(\Zero\Core\Database::getConnection());

Database::getConnection() builds its PDO from the constants in app/config/database.ini, so the entity layer shares the connection the rest of the site uses. Don't call Entity::init() yourself: it returns false once a connection is set.

No Database, No Entity Classes

Because loading Entity.php connects, the first use of any Zero\Entity class on a site with no database configured throws Exception("Database is not configured: set DB_DRIVER, NAME and USER in app/config/database.ini") from core/Database.php, before any connection is tried. A configured database that cannot be reached throws Exception("Database connection failed").