Comments (11)
@mark-gerarts I've added a suggestion for resolving this issue in PR #21 .
from automapper-plus.
Hi @r-alexandr. I've tested your example, and the issue seems fixed with PR #21. I've tagged a new release containing this fix (1.2.2), so if you run a composer update mark-gerarts/auto-mapper-plus
the exception should disappear. If you still run into problems, feel free to reopen.
from automapper-plus.
Could you provide some sample code for this? That would help a lot to figure out this issue!
from automapper-plus.
He's referring to whats happening in this article:
https://www.uvdesk.com/en/blog/doctrine-proxy-objects-lazy-loading/.
The current work around is to use fetch EAGER, I'm not sure if you guys can do anything.
from automapper-plus.
Have you tried calling the getter explicitly?
$config
->registerMapping(...)
->forMember('doctrineProperty', function (YourSourceClass $source) {
return $source->getDoctrineProperty();
});
This should create a proper instance out of the proxy object, if I'm not mistaken.
from automapper-plus.
I think the problem is that these proxy classes appear to be dynamically made by Doctrine so you have no way to register the mapping.
from automapper-plus.
Perhaps a solution could be to, optionally, allow for AutoMapper to look at the parent class of $from
for a matching mapping?
from automapper-plus.
Alright, I understand the situation now. @ErikJson I think looking at the parent class wouldn't help, because the proxy object still won't be initialized. The exception would no longer be thrown, but there wouldn't be any data to map (correct me if I'm wrong).
One solution could be to provide a way to 'prepare' the source object. Something like this for example:
$config->registerMapping(Source::class, Destination::class)
->doBefore(function (Source $source) {
// Ensure the related entity is loaded by Doctrine.
$source->getRelatedEntity();
})
->forMember('relatedEntity', Operation::mapTo(...);
Then we could also provide this behaviour globally using the Options
, so we can avoid duplication.
$options->setBeforeAction(function ($source) {
// Ensure Proxy objects are loaded.
if ($source instanceof Proxy) {
$source->__load();
}
});
The second code snippet would still need a way to circumvent the UnregisteredMappingException for Proxy classes though.
Any thoughts on this? Or some alternatives?
from automapper-plus.
I do the following:
$config->registerMapping(MyEntity::class, MyEntityDTO::class)
->forMember("myEntityProperty", Operation:mapTo(MyEntityDTO::class));
$config->registerMapping(MyEntityDoctrine::class, MyEntityDTO::class)
->copyFrom(MyEntity::class, MyEntityDTO::class);
from automapper-plus.
Reopening because I want to test if #21 actually fixes this issue
from automapper-plus.
I have the same problem with proxy classes of doctrine.
Object A has a reference to object B. I get object A without loading B. I map object A into Adto1. Adto1 has no related objects. It's all good. Then I get object A with forced load B. I map object A into Adto2. Adto2 has an associated Bdto object. I get an error:
"No mapping registered for converting an instance of class Proxies\CG\App\B into one of App\Bdto"
calss A
{
public id int;
public name string;
public b B;
}
calss Adto1
{
public id int;
public name string;
}
calss Adto2
{
public id int;
public name string;
public b Bdto;
}
calss B
{
public id int;
public name string;
}
calss Bdto
{
public id int;
public name string;
}
$config->registerMapping(A::class, Adto1::class);
$config->registerMapping(B::class, Bdto::class);
$config->registerMapping(A::class, Adto2::class)
->forMember('b', Operation::mapTo(Bdto::class));
$entityA = $repository->find(100);
$mapper->map($entityA, Adto1::class);
$entityA1 = $repository->createQueryBuilder('a')
->innerJoin('a.b','b')
->getQuery()
->getOneOrNullResult();
$mapper->map($entityA1, Adto2::class);
from automapper-plus.
Related Issues (20)
- Last version 1.3.11 is probably not minor upgrade :) HOT 5
- doctrine gets entities one by one HOT 1
- Custom Mapper with reverse map HOT 2
- Flattening objects and reusability HOT 2
- Operation::setTo(true) HOT 2
- Map to multiple operation and map from method operation HOT 4
- Update object without creating new HOT 2
- DataLoader for avoid N+1 problems HOT 2
- What about polymorphism? HOT 7
- Support for PHP 7.4 class typed properties mapping HOT 4
- Psalm annotations HOT 3
- Possible typo In README File HOT 5
- Encode a password during mapping in forMember() HOT 1
- MapToObject with nested Objects
- Mapping string into DateTimeImmutable HOT 2
- Migrate CI to Github actions
- Map to object where properties are in a single associative array HOT 1
- Allow context for map() with CustomMapper HOT 5
- Missing skip null fields HOT 2
- Context lost when using mapFrom(...)->mapTo(...) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from automapper-plus.