Download the PHP package andanteproject/nullable-embeddable-bundle without Composer

On this page you can find all versions of the php package andanteproject/nullable-embeddable-bundle. 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 nullable-embeddable-bundle

Andante Project Logo

Nullable Embeddable Bundle

Symfony Bundle - AndanteProject

Latest Version Github actions codecov Framework Php8 PhpStan

A Symfony Bundle that extends Doctrine Embeddables to allow them to be nullable with custom business logic to precisely determine their null state, handling null and uninitialized properties, addressing a common limitation in Doctrine ORM.

Introduction

Doctrine Embeddables are powerful for encapsulating value objects, but they inherently cannot be null. This bundle provides a flexible solution to this limitation by introducing the #[NullableEmbeddable] attribute. This attribute allows you to define custom logic, either through a dedicated static anonymous function (PHP 8.5+), to determine when an embeddable object should be considered null. This enables precise control over the null state, even handling uninitialized properties safely.

The bundle works seamlessly with multiple levels of embedded objects, processing from the deepest leaf embeddable up to the root entity.

For example, a Country embeddable can be marked as nullable based on an uninitialized property:

Requirements

Install

Via Composer:

After installation, make sure you have the bundle registered in your Symfony bundles list (config/bundles.php):

This should be done automatically if you are using Symfony Flex. Otherwise, register it manually.

Usage

The core of this bundle is the #[NullableEmbeddable] attribute, which you place on your Doctrine Embeddable classes alongside #[ORM\Embeddable]. This attribute requires a processor argument, which can be either a class implementing static anonymous function (PHP 8.5+).

Processor Interface

For older PHP versions or more complex logic that warrants a dedicated class, you can implement the ProcessorInterface.

Your processor class must implement this interface.

Example: Address Embeddable with Class Processor

And the corresponding AddressEmbeddableProcessor class:

Anonymous Function Processor (PHP 8.5+)

For projects running on PHP 8.5 or newer, the most convenient way to define your nullability logic is using a static anonymous function directly within the #[NullableEmbeddable] attribute. This keeps your business logic co-located with the embeddable definition, avoiding the need for separate processor classes.

Example: Address Embeddable with Anonymous Function Processor

Consider an Address embeddable that should be considered null if all its properties (street, city, country) are null.

In this example, the anonymous function receives a Result enum (Result::SHOULD_BE_NULL or Result::KEEP_INITIALIZED).

Example: Country Embeddable with Anonymous Function Processor

A nested embeddable like Country can also use this approach. Here, Country is considered null if its code property is uninitialized (meaning it was never set, often indicating a new, empty object).

The PropertyAccessor

The PropertyAccessor provided to your processor (or anonymous function) is a specialized tool that allows you to inspect the state of embeddable properties, including whether they are uninitialized. This is particularly useful for non-nullable properties that might not have been set when an object is retrieved from the database or instantiated.

The Result Enum

The analyze method of your processor must return one of two values from the Result enum:

PHPStan Extension

This bundle includes a PHPStan extension that validates #[NullableEmbeddable] classes to ensure they follow best practices for working with Doctrine's nullable embeddable behavior.

Why This Extension is Important

When Doctrine determines that an entire embeddable object should be null (which is what this bundle does), it sets all the embeddable's database columns to NULL. This has important implications for how you structure your embeddable classes:

  1. Property Initialization: Properties with non-null default values should be initialized in the constructor, not outside it. This is because Doctrine hydrates entities by skipping the constructor. For example:

  2. Nullable Columns: All properties mapped to database columns must be nullable. This can be achieved either by using a PHP nullable type (?string), which Doctrine automatically infers as nullable: true, or by explicitly setting nullable: true in the #[Column] attribute. This is required because when the embeddable object is null, Doctrine will set all its database columns to NULL.

  3. Nested Embeddables with Defaults: Embedded objects that have explicit non-null default values must be typed as nullable. Uninitialized embedded properties are fine since they remain uninitialized when the parent is null.

Automatic Installation (Recommended)

If you have phpstan/extension-installer installed (which is included in require-dev), the extension will be automatically registered. No additional configuration needed!

Manual Installation

If you don't have phpstan/extension-installer, you can manually include the extension in your phpstan.neon or phpstan.neon.dist:

What the Extension Checks

The PHPStan extension will report errors for:

  1. Properties with non-null default values outside the constructor - These should be moved to the constructor to avoid hydration issues
  2. Non-nullable column mappings - Properties with #[Column] must be nullable, either via PHP nullable type (?Type) or explicit nullable: true
  3. Embedded objects with non-null default values - Embedded properties with explicit defaults must be nullable (uninitialized embedded properties are allowed)

Example

Configuration

The bundle provides a configuration option to enable a cache warmer for improved performance in production environments.

Alternatively, using PHP:

Nested Embeddables

This bundle fully supports nested embeddables (e.g., an Address embeddable containing a Country embeddable). The processing logic correctly traverses the embeddable tree, starting from the deepest nested embeddable and working its way up to the root entity.

Built with love ❤️ by AndanteProject team.


All versions of nullable-embeddable-bundle with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
symfony/config Version ^5.0 || ^6.0 || ^7.0 || ^8.0
symfony/http-kernel Version ^5.0 || ^6.0 || ^7.0 || ^8.0
symfony/dependency-injection Version ^5.0 || ^6.0 || ^7.0 || ^8.0
symfony/property-access Version ^5.0 || ^6.0 || ^7.0 || ^8.0
doctrine/orm Version ^2.0 | ^3.0
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 andanteproject/nullable-embeddable-bundle contains the following files

Loading the files please wait ...