Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not mapping proxy classes #3

Closed
yhnehc opened this issue Dec 1, 2017 · 6 comments
Closed

Not mapping proxy classes #3

yhnehc opened this issue Dec 1, 2017 · 6 comments

Comments

@yhnehc
Copy link

yhnehc commented Dec 1, 2017

When you do lazy loading sometimes the objects come back as a proxy class which breaks since there's no mapping config registered for the proxy classes.

Example of the error:
No mapping registered for converting an instance of class Proxies\CG\Bundle\Entity\ProductItem into one of \ProductItemDto

@mark-gerarts
Copy link
Owner

See discussion over at mark-gerarts/automapper-plus#9.

@segidev
Copy link

segidev commented Nov 9, 2018

We have resolved the issue by extending the PropertyAccessor and overwrite the getProperty method.

use AutoMapperPlus\PropertyAccessor\PropertyAccessor;
use Doctrine\ORM\Proxy\Proxy;

class CustomDoctrineProxyPropertyAccessor extends PropertyAccessor
{
    public function getProperty($object, string $propertyName)
    {
        // This is the magical part :)
        if ($object instanceof Proxy) {
            $object->__load();
        }

        return parent::getProperty($object, $propertyName);
    }
}

And then by implementing the AutoMapperConfiguratorInterface and overwrite the configure method

use AutoMapperPlus\AutoMapperPlusBundle\AutoMapperConfiguratorInterface;
use AutoMapperPlus\Configuration\AutoMapperConfigInterface;

class CustomAutoMapperConfigurator implements AutoMapperConfiguratorInterface
{
    // ...
    public function configure(AutoMapperConfigInterface $config): void
    {
        // This call is important
        $config->getOptions()->setPropertyAccessor(new CustomDoctrineProxyPropertyAccessor());
    }
}

Each of our map class extends the CustomAutoMapperConfigurator class eg.

class SomeAssociationMap extends CustomAutoMapperConfigurator
{
    public function configure(AutoMapperConfigInterface $config): void
    {
        $config->registerMapping(Association::class, SomeAssociationModel::class)
            ->forMember('prop', Operation::mapTo(SomeOtherClass::class));
    }
}

That works and we are now automatically loading proxies if they are of that type. The check if the proxy was already intialized prevents extensive reloading of the proxy.

@mark-gerarts
Copy link
Owner

Hi @segidev, thanks for providing your workaround! Just wondering, what version of the base library (mark-gerarts/auto-mapper-plus) are you using? I thought this was resolved in version 1.2.2. So if you are using this version and are still getting errors I might have to reopen the related issue.

@segidev
Copy link

segidev commented Nov 9, 2018

I am out of work now and can’t tell at that moment. Although i think i saw different versions ranging from 1.1.0 to 1.2.3

The thing is i tried to update the dependency for the auto-mapper-bundle for symfony with dependencies and there was no update scheduled. So probably the issue still exists. But i will let you know on monday.

We are using Symfony 3 if that is somehow important :)

@segidev
Copy link

segidev commented Nov 22, 2018

For several reasons that have nothing to do with this bundle we stopped implementing the library so i can’t invest further here. You can close this thread. I just wanted you to know :)

Thank you !

@mark-gerarts
Copy link
Owner

Thanks for letting me know :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants