-
Notifications
You must be signed in to change notification settings - Fork 11
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
Comments
See discussion over at mark-gerarts/automapper-plus#9. |
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. |
Hi @segidev, thanks for providing your workaround! Just wondering, what version of the base library ( |
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 :) |
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 ! |
Thanks for letting me know :) |
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
The text was updated successfully, but these errors were encountered: