Entity Framework
An optional EAV data layer that ships with Zero's core. Two tables hold every type you define; each type gets a pivoted SQL view you can query like a normal table.
Put a class that extends \Zero\Entity\Entity in your site's entity/ folder, or call GenericEntity::define() to have one generated. The first new MyType([...]) call inserts the type, adds a MySQL partition, writes the schema, saves the record and builds the view.
What is the Entity Framework?
The Entity Framework is a small EAV (Entity-Attribute-Value) data layer for Zero: no migration and no new column for each new field. You declare a class, hand it a data array, and the keys of that array become the type's attributes.
It rests on two MySQL tables, entity_type and entity, both created by core/sql/core.sql. Each type gets its own partition of the entity table and a generated pivot view ({label}_view) that gives SELECTs the normal row-per-record shape.
Zero's core has a Model base class over PDO (see Models) and nothing requires this layer. Use it where its trade-offs fit, and plain PDO with your own tables everywhere else. This site has no database and runs none of the listings in this section.
Where Things Live
| What | Where |
|---|---|
The framework: Entity, GenericEntity, Event |
core/entity/, a git submodule of core. Namespace Zero\Entity. |
| Your own entity classes, written by hand or generated | The site's entity/ folder, or the folder named by the constant ENTITY_SITE_DIR. Same namespace. |
The tables, the event type and event_view |
core/sql/core.sql |
| The admin screens | The bundled Entity module, core/modules/Entity/, at /admin/entity. It is one of the bundled modules, on by default, and can be switched off in app/modules.ini. |
No require_once is needed. Application::classFile() in core/Application.php resolves Zero\Entity\X to core/entity/X.php first, then to the site's entity/X.php. A site class named like a framework class is never loaded. See Autoloading.
How It Works
The first constructor call for a class with no entity_type row defines the schema: the keys of the data array become the attribute names, in order. A later call with a key the schema does not have appends it and rebuilds the view. Nothing removes an attribute.
Core Topics
database Storage Model
How entity_type, entity, and the pivoted {label}_view fit together.
add_box Creating Types
Hand-written subclass, GenericEntity::define(), or the admin form.
search Querying
Load by id, find by attribute, or read the pivoted view in SQL.
auto_awesome Default Values
date:, uniqid:, server:, or a plain value, written into the generated constructor.
warning Footguns
The 255-character value, schema-on-write, and the three shapes where() returns.
code Examples
Core's own Event class, and a generated type from definition to query.
Minimal Example
A hand-written entity class is just a marker. Its lowercased class name is the type's label:
<?php
namespace Zero\Entity;
class Note extends Entity {
public $uniq = ['title'];
}
Use it:
// The class autoloads; loading Entity connects through Database::getConnection()
// First call defines the schema: title, body, created_at
$n = new \Zero\Entity\Note([
'title' => 'First Note',
'body' => 'Hello, world.',
'created_at' => date('Y-m-d H:i:s'),
]);
// Later calls reuse the schema
$another = new \Zero\Entity\Note([
'title' => 'Second Note',
'body' => 'More text.',
'created_at' => date('Y-m-d H:i:s'),
]);
The first call inserts the note row in entity_type, adds its partition to the entity table, writes the schema rows, stores the record and creates note_view. Every later call just writes data.
If the first call's array has the wrong keys or the wrong order, that is the schema. Fixing it is manual SQL on the type's id = 0 rows. See Footguns.
When to Reach for It
Use it for small-to-medium, schema-fluid records where a migration for every new field is not wanted: logging (core's own Event), admin-defined types, one-off test fixtures.
Skip it when you need typed columns, joins, transactions, values longer than 255 characters, high write throughput or a real uniqueness constraint. Every value is a VARCHAR(255) string, every write is a REPLACE INTO on a MyISAM table, and $uniq is a SELECT before the write. Core itself made that call for accounts: users are Zero\Core\Access\User over a users table with a real unique key on email, not an entity. For a type like that, give it its own table and extend Zero\Core\Model (Models).