Download the PHP package pechynho/polymorphic-doctrine without Composer

On this page you can find all versions of the php package pechynho/polymorphic-doctrine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package polymorphic-doctrine

Polymorphic Doctrine

A Symfony bundle that brings polymorphic relations to Doctrine ORM. It allows a single entity property to reference different entity types — with type safety, IDE support, and optional foreign key constraints.

License: MIT

The Problem

Doctrine ORM doesn't natively support a property that needs to reference multiple unrelated entity types. Imagine a Payment entity with a subject property that can point to either an EshopItem or a Subscription. Standard Doctrine associations require a fixed target type.

This bundle solves it with two approaches:

Mode DB Columns Foreign Keys Best For
Dynamic {property}_type + {property}_id No Flexible scenarios where FK constraints aren't needed
Explicit {property}_type + one ID column per mapped type Yes Scenarios requiring referential integrity

Installation

Without Symfony Flex, add the bundle to config/bundles.php:

Quick Start

1. Mark the entity and define polymorphic properties

2. Generate reference classes (explicit mode only)

3. Work with polymorphic values

Polymorphic Modes

Dynamic Mode

Creates two columns per polymorphic property:

id dynamic_subject_type dynamic_subject_id
1 eshop_item 123
2 subscription 456
3 NULL NULL

Pros: Simple setup, no class generation needed. Cons: No foreign keys — the database cannot enforce referential integrity.

Explicit Mode

Creates a type column plus a dedicated ID column for each mapped entity type:

id explicit_subject_type explicit_subject_eshop_item_id explicit_subject_subscription_id
1 eshop_item 123 NULL
2 subscription NULL 456
3 NULL NULL NULL

Pros: Foreign key on each ID column, referential integrity enforced by the database. Cons: Requires reference class generation, more columns per table.

Working with Polymorphic Values

Creating and Initializing

Updating

Reading

Tip: Always check isNull() before calling getValue().

Type-Narrowing with getValueAs()

The getValueAs() method provides a convenient way to retrieve the referenced entity with a narrowed return type. It accepts a class-string<T> and returns T, giving you full IDE autocompletion and static analysis support (PHPStan / Psalm).

The method throws a ReferenceResolutionException if:

Searching Polymorphic Values

The bundle provides two approaches for querying polymorphic properties via QueryBuilder.

Applier — Simple Approach

Directly modifies the QueryBuilder. Best for straightforward queries.

Builder — Advanced Approach

Returns expression objects with parameters that can be composed into complex queries.

Available Operations

Method Description
eq($entity) Matches the given entity
neq($entity) Does not match the given entity
in(...$entities) Matches any of the given entities
notIn(...$entities) Does not match any of the given entities
isNull() Value is null
isNotNull() Value is not null
isInstanceOf(...$classes) Is an instance of the given type(s)
isNotInstanceOf(...$classes) Is not an instance of the given type(s)

Configuration

API Reference

Attributes

#[EntityWithPolymorphicRelations]

Marks an entity as containing polymorphic relations. Required for entity discovery.

#[DynamicPolymorphicProperty(array $mapping)]

Defines a dynamic polymorphic property (two columns: type + ID).

Parameter Type Description
$mapping array Map of type keys to entity class names
$idProperty ?string Custom ID property name
$enableDiscriminatorIndex ?bool Add index on discriminator column
$enablePairIndex ?bool Add index on type + ID pair

#[ExplicitPolymorphicProperty(array $mapping)]

Defines an explicit polymorphic property (type column + dedicated ID column per mapped type).

Parameter Type Description
$mapping array Map of type keys to entity class names or detailed config
$idProperty ?string Default ID property name
$idPropertyType ?string Default ID property type
$onDelete ?string Foreign key ON DELETE action
$onUpdate ?string Foreign key ON UPDATE action
$enableDiscriminatorIndex ?bool Add index on discriminator column
$enablePairIndex ?bool Add index on type + ID pair

Per-type detailed configuration in the mapping:

PolymorphicValueInterface

Method Return Type Description
isNull() bool Is the value null?
isResolvable() bool Can the value be resolved to an entity?
isLoaded() bool Is the entity already loaded in memory?
getValue() ?object Returns the referenced entity (lazy-loaded)
getValueAs(string $fqcn) T Returns the entity narrowed to the given type, or throws
update(?object $value) void Updates the reference to another entity or null
setNull() void Sets the reference to null

Services

Service Description
PolymorphicValueFactoryInterface Creates polymorphic value instances
PolymorphicSearchExprBuilderFactoryInterface Creates search expression builders
PolymorphicSearchExprApplierFactoryInterface Creates appliers that directly modify QueryBuilder

Console Commands

Important: After any change to explicit polymorphic property definitions, you must re-run the reference class generation command.

Troubleshooting

Problem Solution
Reference classes not found (explicit mode) Run php bin/console pechynho:polymorphic-doctrine:generate-reference-classes then php bin/console cache:clear
Polymorphic properties not working Verify the #[EntityWithPolymorphicRelations] attribute is on the entity, the bundle is registered, and discovery directories are configured
Slow queries Enable indexes via enableDiscriminatorIndex and enablePairIndex on your attributes

Requirements

License

MIT — see LICENSE.

Author

Jan Pech — [email protected]


All versions of polymorphic-doctrine with dependencies

PHP Build Version
Package Version
Requires php Version >=8.4
doctrine/doctrine-bundle Version ^2.12
webmozart/assert Version ^1.11
symfony/cache-contracts Version ^3.6
symfony/service-contracts Version ^3.6
nette/php-generator Version ^4.1
spatie/php-structure-discoverer Version ^2.3
symfony/filesystem Version ^6.4 || ^7.0 || ^8.0
symfony/http-kernel Version ^6.4 || ^7.0 || ^8.0
symfony/property-access Version ^6.4 || ^7.0 || ^8.0
symfony/console Version ^6.4 || ^7.0 || ^8.0
symfony/dependency-injection Version ^6.4 || ^7.0 || ^8.0
symfony/options-resolver Version ^6.4 || ^7.0 || ^8.0
symfony/string Version ^6.4 || ^7.0 || ^8.0
symfony/config Version ^6.4 || ^7.0 || ^8.0
doctrine/orm Version ^2.19 || ^3.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package pechynho/polymorphic-doctrine contains the following files

Loading the files please wait ...