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

Added support for Doctrine Embedabbles #22

Closed driehle closed 2 years ago

driehle commented 3 years ago

Summary: Added support for Doctrine Embedabbles

BC Breaks: None

Note: This is related to #18. However, in contrast to #18, this does neither directly extract fields from embeddables nor hydrate them. Instead, this extracts or hydrates the object from the embeddable. In consequence, this deals with embedabbles in a similar way as with a toOne relationship and requires a separat hydrator for the inner fields of the embeddable.

Using Laminas\Form this allows to create a corresponding fieldset to each embeddable and, hence, enables re-using these fieldsets accros all entities that utilize the same embeddable. See the following example of an AddressFieldset, which can be adopted in other fieldsets or forms:

<?php

namespace Application\Form\Fieldset;

use Application\Entity\Address;
use Doctrine\Laminas\Hydrator\DoctrineObject as DoctrineHydrator;
use Doctrine\Persistence\ObjectManager;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use DoctrineModule\Persistence\ProvidesObjectManager;
use Laminas\Filter\StringTrim;
use Laminas\Filter\ToNull;
use Laminas\Form\Element\Select;
use Laminas\Form\Fieldset;
use Laminas\InputFilter\InputFilterProviderInterface;

class AddressFieldset extends Fieldset implements ObjectManagerAwareInterface, InputFilterProviderInterface
{
    use ProvidesObjectManager;

    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('fieldset-edit-address');
        $this->setObjectManager($objectManager);
    }

    public function init()
    {
        $this->setHydrator(new DoctrineHydrator($this->getObjectManager()));
        $this->setObject(new Address());

        $this->add([
            'name' => 'street',
            'options' => [
                'label' => 'Street and Number',
            ],
        ]);

        $this->add([
            'name' => 'postCode',
            'options' => [
                'label' => 'Post Code',
            ],
        ]);

        $this->add([
            'name' => 'city',
            'options' => [
                'label' => 'City',
            ],
        ]);
    }

    public function getInputFilterSpecification()
    {
        return [
            'street' => [
                'required' => false,
                'filters' => [
                    ['name' => StringTrim::class],
                    ['name' => ToNull::class],
                ],
            ],
            'postCode' => [
                'required' => false,
                'filters' => [
                    ['name' => StringTrim::class],
                    ['name' => ToNull::class],
                ],
            ],
            'city' => [
                'required' => false,
                'filters' => [
                    ['name' => StringTrim::class],
                    ['name' => ToNull::class],
                ],
            ],
        ];
    }
}

Maybe this should be stated in the documentation somewhere?

driehle commented 3 years ago

@TomHAnderson @greg0ire Any chance on pushing this forward?

TomHAnderson commented 3 years ago

Thanks for the bump, @driehle

Is there any documentation for this new feature that should be added to the README?

driehle commented 3 years ago

@TomHAnderson I have added an example to the Readme file, is that sufficient?

driehle commented 3 years ago

@TomHAnderson push

driehle commented 3 years ago

@TomHAnderson @greg0ire Could someone please have a look at this to get this merged?

driehle commented 2 years ago

@greg0ire Could you please review again the changes?

driehle commented 2 years ago

@TomHAnderson @greg0ire Can we proceed with having this merged?

greg0ire commented 2 years ago

This is a new feature and as such should target 2.2.x

driehle commented 2 years ago

@greg0ire absolutely, yes, but there is no such branch yet, hence, I can't set the target accordingly.

greg0ire commented 2 years ago

Oh :sweat_smile:

Let me create it for you :)

greg0ire commented 2 years ago

Dang, I don't have the required permissions. Let me ask around.

TomHAnderson commented 2 years ago

2.2.x has been created

driehle commented 2 years ago

@TomHAnderson Thanks for creating the branch. Can you merge this now or is there anything else required?