doctrine / doctrine-laminas-hydrator

Doctrine hydrators for Laminas applications
https://www.doctrine-project.org/projects/doctrine-laminas-hydrator.html
MIT License
33 stars 19 forks source link

Documentation issues and hydration apparently not working for Embeddable #61

Closed cbichis closed 1 year ago

cbichis commented 1 year ago

Hi,

I have checked the documentation for for 3.1.0.

https://www.doctrine-project.org/projects/doctrine-laminas-hydrator/en/3.1/basic-usage.html#basic-usage

For embedded case I see couple problems:

  1. The class Tag is actually Address (according to Person class)
  2. Hydrating example doesnt work. For new person object both address fields are null. One problem is the hydrating array is wrong:
[
    [
        'postalCode' => '48149',
        'city' => 'Münster',
    ],
],

it should be:

[
    'postalCode' => '48149',
    'city' => 'Münster',
],
  1. Both fields of Address are still null after hydrating even after fixing the above errors. The cause I guess is because

$metadata->hasAssociation($field) and is_callable([$object, $setter]) are false within hydrateByValue function... They are not working properly for Embeddable.

  1. I added the missing setAddress function but I am getting issues:
public function setAddress(Address $address): void
{
    $this->address = $address;
}

Person::setAddress(): Argument #1 ($address) must be of type Address, array given

  1. Only If I am creating a
    public function setAddress(Address | array $address): void
    {
        if(is_array($address)){
            print_r($address);

            $this->address->setCity($address['city']);
            $this->address->setPostalCode($address['postalCode']);
        } else {
            $this->address = $address;
        }
    }

the code is working properly and the Address is hydrate. Not sure if this is the expected behavior but I dont see it useful this way.

Testing setup:

doctrine/annotations 1.13.3 Docblock Annotations Parser doctrine/cache 1.13.0 PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mo... doctrine/collections 1.7.0 PHP Doctrine Collections library that adds additional functionality on top of PHP arrays. doctrine/common 3.3.1 PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as bet... doctrine/dbal 2.13.9 Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management. doctrine/deprecations v0.5.3 A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively ... doctrine/doctrine-laminas-hydrator 3.1.0 Doctrine hydrators for Laminas applications doctrine/doctrine-module 5.2.0 Laminas Module that provides Doctrine basic functionality required for ORM and ODM modules doctrine/doctrine-orm-module 5.2.0 Laminas Module that provides Doctrine ORM functionality doctrine/event-manager 1.1.2 The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects. doctrine/inflector 2.0.4 PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural... doctrine/instantiator 1.4.1 A small, lightweight utility to instantiate objects in PHP without invoking their constructors doctrine/lexer 1.2.3 PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers. doctrine/migrations 3.4.2 PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your... doctrine/orm 2.13.1 Object-Relational-Mapper for PHP doctrine/persistence 3.0.3 The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share.

laminas/laminas-authentication 2.11.0 provides an API for authentication and includes concrete authentication adapters for common use case scenarios laminas/laminas-cache 3.4.0 Caching implementation with a variety of storage options, as well as codified caching strategies for callbacks, classes, and output laminas/laminas-cache-storage-adapter-filesystem 2.0.1 Laminas cache adapter for filesystem laminas/laminas-cache-storage-adapter-redis 2.2.0 Laminas cache adapter for redis laminas/laminas-cli 1.5.0 Command-line interface for Laminas projects laminas/laminas-code 4.6.0 Extensions to the PHP Reflection API, static code scanning, and code generation laminas/laminas-component-installer 2.8.0 Composer plugin for injecting modules and configuration providers into application configuration laminas/laminas-config 3.7.0 provides a nested object property based user interface for accessing this configuration data within application code laminas/laminas-crypt 3.8.0 Strong cryptography tools and password hashing laminas/laminas-db 2.15.0 Database abstraction layer, SQL abstraction, result set abstraction, and RowDataGateway and TableDataGateway implementations laminas/laminas-development-mode 3.8.0 Laminas development mode script laminas/laminas-di 3.9.1 Automated dependency injection for PSR-11 containers laminas/laminas-diactoros 2.14.0 PSR HTTP Message implementations laminas/laminas-escaper 2.10.0 Securely and safely escape HTML, HTML attributes, JavaScript, CSS, and URLs laminas/laminas-eventmanager 3.5.0 Trigger and listen to events within a PHP application laminas/laminas-filter 2.17.0 Programmatically filter and normalize data and files laminas/laminas-form 3.4.1 Validate and display simple and complex forms, casting forms to business objects and vice versa laminas/laminas-http 2.16.0 Provides an easy interface for performing Hyper-Text Transfer Protocol (HTTP) requests laminas/laminas-hydrator 4.5.0 Serialize objects to arrays, and vice versa laminas/laminas-i18n 2.17.0 Provide translations for your application, and filter and validate internationalized values laminas/laminas-inputfilter 2.19.1 Normalize and validate input sets from the web, APIs, the CLI, and more, including files laminas/laminas-json 3.3.0 provides convenience methods for serializing native PHP to JSON and decoding JSON to native PHP laminas/laminas-loader 2.8.0 Autoloading and plugin loading strategies laminas/laminas-log 2.15.2 Robust, composite logger with filtering, formatting, and PSR-3 support laminas/laminas-mail 2.17.0 Provides generalized functionality to compose and send both text and MIME-compliant multipart e-mail messages laminas/laminas-math 3.5.0 Create cryptographically secure pseudo-random numbers, and manage big integers laminas/laminas-mime 2.9.1 Create and parse MIME messages and parts laminas/laminas-modulemanager 2.12.x-dev 2a3d943 Modular application system for laminas-mvc applications laminas/laminas-mvc 3.3.4 Laminas's event-driven MVC layer, including MVC Applications, Controllers, and Plugins laminas/laminas-mvc-form 2.1.x-dev 6f46662 Metapackage with all requirements needed to use laminas-form with laminas-mvc. laminas/laminas-mvc-i18n 1.4.0 Integration between laminas-mvc and laminas-i18n laminas/laminas-mvc-plugin-flashmessenger 1.8.1 Plugin for creating and exposing flash messages via laminas-mvc controllers laminas/laminas-mvc-plugin-identity 1.4.0 Plugin for retrieving the current authenticated identity within laminas-mvc controllers laminas/laminas-navigation 2.14.0 Manage trees of pointers to web pages in order to build navigation systems laminas/laminas-paginator 2.13.0 Paginate collections of data from arbitrary sources laminas/laminas-permissions-acl 2.10.0 Provides a lightweight and flexible access control list (ACL) implementation for privileges management laminas/laminas-psr7bridge 1.6.0 Bidirectional conversions between PSR-7 and laminas-http messages laminas/laminas-router 3.7.0 Flexible routing system for HTTP and console applications laminas/laminas-serializer 2.13.0 Serialize and deserialize PHP structures to a variety of representations laminas/laminas-servicemanager 3.16.0 Factory-Driven Dependency Injection Container laminas/laminas-session 2.13.0 Object-oriented interface to PHP sessions and storage laminas/laminas-stdlib 3.11.0 SPL extensions, array utilities, error handlers, and more laminas/laminas-uri 2.9.1 A component that aids in manipulating and validating » Uniform Resource Identifiers (URIs) laminas/laminas-validator 2.23.0 Validation classes for a wide range of domains, and the ability to chain validators to create complex validation criteria laminas/laminas-view 2.22.1 Flexible view layer supporting and providing multiple view layers, helpers, and more

cbichis commented 1 year ago

Trying to hydrate by reference without the setAddress function generates this:

Cannot assign array to property Person::$address of type Address {"file":"/project/vendor/doctrine/doctrine-laminas-hydrator/src/DoctrineObject.php","line":436

driehle commented 1 year ago

Could you share a full code example of your non-working case?

driehle commented 1 year ago

Could you please check if your issue is related to doctrine/orm#10041? Please upgrade orm to 2.13.2 an see if your issue persists.

cbichis commented 1 year ago

I'll check. Also I'll provide a sample, just need some time.

cbichis commented 1 year ago

Hi,

I think hydrating embeddable works, but we need some proper Annotation\Instance in place.

I'll close this for now.