stave.dev

Footguns

The sharp edges: things that look like they work but don't, and the specific failure when they bite.

Read This Once, Then Keep Building

Each of these is how the code in core/entity/ behaves today, checked against Entity.php and GenericEntity.php. They are the places where new code tends to go wrong.

1. where() Returns Three Shapes

where() returns null, one Entity, or an array of them, depending on how many records matched. There is no "always an array" mode. A key the type does not have also returns null, with a Console warning, so a typo in the key looks exactly like "not found".

$r = \Zero\Entity\Product::getInstance()->where('sku', $sku);

if ($r === null)          { /* not found, or unknown key */ }
elseif (is_array($r))     { /* several matches */ }
else                      { /* single Entity */ }

2. A Value Is 255 Characters

Every attribute value is stored in entity.value, a VARCHAR(255). Entity does no type or length check before it writes, so what happens to a longer value is left to MySQL. Only Event guards itself: it cuts a detail over 255 characters to fit and logs the cut (core/entity/Event.php).

json_encode output, prose, long URLs and most log messages will not fit. Give that data its own table (Models).

3. The First Constructor Call Defines the Schema

new SomeEntity($data) for an unregistered type creates it from array_keys($data). After that, any key or property that is not in the schema is appended to it, so a typo in an attribute name becomes a permanent attribute. Nothing removes or renames one: that is manual SQL on the type's id = 0 rows, and the next attribute index is taken from the count of those rows, so they must stay numbered without gaps.

4. define() Stores a Record of Nulls

GenericEntity::define() registers the type by constructing one instance with every attribute set to null, and that instance is stored. Each run adds one such record. With $force = true it regenerates the class file and appends attributes that are new, but never removes or renames old ones.

5. new X() Can Store a Record

A generated class with defaults fills them into the data array, and a non-empty data array stores a record. So new Product(), meant as an empty object to call where() on, writes a record of defaults every time. Use Product::getInstance(), which does not run the subclass constructor.

6. withId() Never Returns null

For an id that does not exist, withId() returns the instance with id left null. Check $found->id, not the return value.

7. $uniq Is a SELECT, Not a Constraint

The duplicate check runs in __set() before the write. Two concurrent writes can both pass it. It also does not exclude the record being written, so assigning a unique attribute its own current value throws Duplicate entry found; the message carries the id of the row it collided with.

8. An Underscore in a Type Name Is Lost in the Label

define('product_item', ...) generates ProductItem, which stores under the label productitem with the view productitem_view. new GenericEntity('product_item', ...) stores under product_item, a second type. After define(), use the generated class only.

9. A Property Added Later Is Missing From the View

Assigning a property that is new to the schema on an existing record stores the schema row and the value, but does not rebuild the view. The column appears after forceUpdateView(), or the next time a constructor appends an attribute.

10. A Type Named user Replaces user_view

updateView() runs DROP VIEW IF EXISTS {label}_view before it rebuilds. core/sql/core.sql creates user_view as a compatibility view over the users table, and nothing refuses the type name user, so defining it replaces that view with an EAV pivot. Nothing in core reads user_view, so nothing fails visibly. Recovery is re-running core/sql/migrations/user_view.sql. The same applies to any view of yours named {something}_view.

11. A Site Class Cannot Replace a Framework Class

Zero\Entity\X is core/entity/X.php first, then the site's entity/X.php (Application::classFile()). A site file named Event.php is never loaded, and define() refuses a type whose class name matches a file in core/entity/.

12. Hand-Written Classes Skip Name Validation

GenericEntity::define() validates every name before it writes anything. A hand-written subclass has no such gate: its attribute names are whatever keys the data array carries. updateView() throws Exception("Unsafe SQL identifier: ...") for a name that does not match /^[a-z_][a-z0-9_]*$/i, but by then the schema row is already written. Never pass user-supplied keys to a constructor unchecked.

Debugging Playbook

Symptom Likely Cause Fix
"Database is not configured: set DB_DRIVER, NAME and USER in app/config/database.ini" Loading core/entity/Entity.php connects through Database::getConnection(), and DB_DRIVER, NAME or USER is blank or absent. DB_DRIVER has no default. Set the three keys (Models & Database). There is no require_once or init() call to add.
"Database connection failed" The database is configured and cannot be reached: wrong credentials, host or driver Fix app/config/database.ini. The PDO message is in the log at critical level.
"Undefined entity type: X" new GenericEntity('X', ...) or GenericEntity::type('X') in a request that did not call define('X', ...) Use the generated class: new \Zero\Entity\X(...), X::getInstance().
"Class file already exists: … use $force=true to overwrite" define() ran a second time for the same type Define once. Pass $force = true only to regenerate on purpose.
"Entity name 'X' is reserved" The class name matches a file in core/entity/ or a PHP reserved word Pick another type name.
"Duplicate entry found for X with id N" $uniq found the value on record N, which may be the record being written Correct the input, or skip the assignment when the value has not changed.
"Unsafe SQL identifier: X" An attribute name outside [a-z0-9_] reached the schema Fix the id = 0 schema row in SQL, then forceUpdateView().
"No entity instance…" on assignment A property was set on an instance with no record: a getInstance() result, or a withId() that found nothing Check id before writing.
Record saves but the attribute is missing from the view The attribute was first created by a property assignment $entity->forceUpdateView()
where() returns null for a record that exists The key is not an attribute of the type; the Console log has the warning Check the key against the type's id = 0 rows.

Reset (Dev Only)

# from the site root
mysql <database> < core/entity/reset_entity.sql

Truncates entity_type and entity and resets the partitioning of entity to one partition, e_type1. That removes the seeded event type along with everything else. Its last block drops every view of the database named in @database_name except user_view; the script does not assign that variable, so the block drops nothing until you do. Generated class files in the site's entity/ folder are not touched. Destructive. Do not run it on a production database.