fix(database): support union-typed entity columns via explicit Column type#145
Open
TuVanDev wants to merge 1 commit into
Open
fix(database): support union-typed entity columns via explicit Column type#145TuVanDev wants to merge 1 commit into
TuVanDev wants to merge 1 commit into
Conversation
… type EntityMetadataFactory rejected any entity property whose reflection type is not a single named type, throwing "must have a type declaration" for union types such as `int|string`. Because db:migrate/db:diff parse metadata for every discovered entity, marko/media's MediaAttachment (a polymorphic foreign key declared `int|string $attachableId`) broke both commands for any project with marko/media installed, regardless of whether the entity was used. Handle ReflectionUnionType: a union has no single reflection type to infer a column type from, so require an explicit #[Column(type: ...)] and use it as the database type. Declare the media_attachments.attachable_id column as varchar, which holds both int and string primary keys while preserving the polymorphic union on the PHP side.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EntityMetadataFactorythrewProperty '...' must have a type declarationfor any entity property with a PHP union type (ReflectionUnionType), with no escape hatch. This brokedb:migrate/db:diffoutright for any project withmarko/mediainstalled, sinceMarko\Media\Entity\MediaAttachment::$attachableIdis declaredint|string(a polymorphic foreign key — an attachment can belong to entity types whose primary keys are int or string).Fix
EntityMetadataFactorynow accepts aReflectionUnionTypeproperty when it carries an explicit#[Column(type: ...)]attribute (already a supported mechanism for named-type columns, just not consulted before the union-type guard threw).MediaAttachment::$attachableIdis now declared#[Column(type: 'varchar', length: 255)], preserving itsint|stringPHP type while giving the factory an unambiguous DB column type. Union properties without an explicit#[Column(type:...)]still throw, now with a clearerEntityException::unionTypeRequiresColumnType()message instead of the previous misleading "must have a type declaration".The
ReflectionNamedTypecode path is unchanged — this is a strictly additive branch.Notes for reviewers
attachableIdalways comes back as a PHP string regardless of the original id's type (varchar columns hydrate to string, andint|stringaccepts that with no TypeError) — code comparing it against a real int PK should use loose comparison or cast explicitly. No existing usage inpackages/mediadoes a strict comparison.stringin their type list (a varchar column round-trips fine). A hypothetical future union likeint|boolwould still need casting support at hydration time — out of scope here since no such entity currently exists.packages/mediaships no migration files — schema is derived entirely from entity metadata viaSchemaRegistry, so this fix directly unblocksdb:diff/db:migratefor any consumer.Test plan
./vendor/bin/pest packages/database/tests— 883 passed./vendor/bin/pest packages/media/tests— 38 passeddevelop(pre-fix) with the original misleading error, and pass on this branch./vendor/bin/phpcs --standard=phpcs.xmlclean on all changed files./vendor/bin/phpstan— zero new errors vsdevelopbaseline