shirakaba-add-entity
Add a database entity to a Shirakaba application - the readonly entity class with mapping attributes, relations, a generated migration, and checks that the schema matches. Use when asked to add a tabl
SKILL.mdpackages/docs/skills/shirakaba-add-entity/SKILL.md
GitHubAdd an entity to a Shirakaba application
Read vendor/shirakaba/docs/guide/06-database.md (Entities, Migrations) and
vendor/shirakaba/docs/design/08-database.md first. Shirakaba entities are plain readonly objects: no lazy loading,
no unit of work, no save().
- Look at what exists.
app entitiesandapp entities:show <a similar entity>show tables, columns, relations and scopes. - Entity class.
#[Table('notes')]on afinal readonlyclass; properties map to columns (camelCase to snake_case unless#[Column('name')]). The key is#[Id, Generated] public ?int $id = nulllast;#[CreatedAt],#[UpdatedAt],#[Version]are managed columns. UseDateTimeImmutableandLocalDate, backed enums, value objects. - Relations. Declare them on the class:
#[BelongsTo('user', User::class, 'userId')],#[HasMany(...)],#[HasOne(...)],#[ManyToMany(..., through: 'note_files', orderBy: 'position')]. Tenant data gets#[RequiredScope(InTenant::class)]. - Register the class in the list the application gives to
Migrations(and toEntityCommandsif it registers them). - Migration. Generate it from the mapping:
vendor/bin/shirakaba-migrate diff --config=migrations.php --class=CreateNotes --namespace='App\Migrations'(orapp db:diff ...). Review it, add indexes for the queries you will run, and add it to the migration list. - Apply and check.
app db:migrate, thenapp db:lintandapp entities:check(orSchemaCheck::entities()in a test). - Read it through projections.
Entities::query(Note::class)->with('user')->into(NoteView::class, $db); write withEntities::insert(),update(),patch().
Verify
vendor/bin/phpstan analyse: shirakaba/phpstan checks property and relation names in queriesapp db:diff --checkexits 0 (no missing migration)app db:lintandapp entities:checkreport nothing- the test suite, against SQLite and, if the project runs them, MySQL and PostgreSQL