Querying Entities
Three ways to read, from narrowest to broadest: load by id, find by attribute, or go straight to SQL against the pivoted view.
Start From getInstance()
Both lookups are instance methods, so they need an instance that holds no record. That is getInstance():
$blank = \Zero\Entity\Product::getInstance();
getInstance() runs Entity's constructor with no data and skips the subclass's own constructor (core/entity/Entity.php), so no defaults are applied and nothing is stored. new Product() is not the same thing: a generated class with defaults fills them in, and data in the constructor stores a record.
Load by ID
$p = \Zero\Entity\Product::getInstance()->withId(42);
if ($p->id === null) {
// no record 42
}
echo $p->name;
withId(int $id): Entity loads the record's rows into the instance, in one query scoped to the type, and returns that same instance. For an id with no rows it still returns the instance, with id left null. It never returns null itself, so check id.
Find by Attribute: where()
$prod = \Zero\Entity\Product::getInstance()->where('sku', 'WDG-1');
The signature is where(string $key, string $val): Entity|array|null. It is an exact match on one attribute's value. $key is an attribute name, or an attribute index when it is numeric.
| Matches | Returns |
|---|---|
| None | null |
| One | One instance of the class it was called on |
| Several | An array of those instances |
A $key the type does not have |
null, after a Console warning that names the class. Nothing is printed and nothing is thrown. |
The query is scoped to the type, so another type's rows never match. Each result is a fresh blank instance of the same class with the record loaded into it; a lookup stores nothing.
There is no "always an array" mode. Check what came back before using it.
$r = \Zero\Entity\Product::getInstance()->where('status', 'active');
if ($r === null) {
// No match
} elseif (is_array($r)) {
// Several matches
foreach ($r as $product) { /* ... */ }
} else {
// Exactly one match — $r is a Product
echo $r->name;
}
Richer Queries: Read the View
For anything beyond a single-attribute exact match (ranges, ordering, joins, pagination, aggregation) query the pivoted view. Every column is a VARCHAR, so cast where a number or date comparison matters:
SELECT * FROM product_view WHERE CAST(price AS DECIMAL(10,2)) > 10 ORDER BY created_at DESC LIMIT 50;
From PHP, use the site's shared connection:
$db = \Zero\Core\Database::getConnection();
$stmt = $db->prepare(
"SELECT id, name, price FROM product_view WHERE status = ? ORDER BY id DESC LIMIT 50"
);
$stmt->execute(['active']);
$rows = $stmt->fetchAll();
Core does the same. core/modules/Admin/model/EventLog.php reads event_view for the admin audit log at /admin/audit, and core/modules/Entity/model/EntityStore.php reads the two EAV tables directly to list, count and page records of any type for /admin/entity/manage/{label}.
Writes and Updates
The class API is thin. Property assignment is an immediate REPLACE INTO:
$p = \Zero\Entity\Product::getInstance()->withId(7); $p->price = '19.99'; // __set writes a REPLACE INTO immediately $p->status = 'discontinued'; // each assignment is its own SQL statement $p->delete(); // deletes the record's rows, scoped by type
There is no save(), no dirty tracking, no transaction. Every assignment is a round trip. That is fine for admin screens and CRUD forms; it is not fine for a loop over 10,000 rows. delete() throws when the instance has no id. Reading an attribute the instance does not have gives null.
Schema-on-Write
Assigning an attribute that is not in the schema creates it:
$p = \Zero\Entity\Product::getInstance()->withId(7); $p->gift_wrap = 'yes'; // attr 'gift_wrap' is not in the schema // Entity::__set(): // 1. INSERT a new schema row (id=0, attr=N, value='gift_wrap') // 2. REPLACE INTO for this record's new attribute // product_view does not have the column yet: $p->forceUpdateView(); // drop and rebuild product_view
A new key in a constructor's data array is appended the same way, and there the constructor rebuilds the view itself.
A typo in an attribute name becomes a permanent attribute of the type. Nothing in the framework freezes a schema, so validate keys that come from user input against the attributes you expect before you assign them.
Unique Attributes
A class lists its unique attributes in $uniq; define() writes it from the uniq key of its config.
class Note extends Entity {
public $uniq = ['title'];
}
Before a unique attribute is written, __set() selects any row of the type with the same attribute and value and throws Exception("Duplicate entry found for {attr} with id {id}") when one exists. The check is a SELECT before the REPLACE, not a database constraint, so two concurrent writes can both pass it. It also does not exclude the record being written: assigning a unique attribute its own current value throws. When uniqueness must hold, use a table with a real unique key instead (Models).