Git Product home page Git Product logo

Comments (11)

mschop avatar mschop commented on June 18, 2024 1

@mark-gerarts I've added a suggestion for resolving this issue in PR #21 .

from automapper-plus.

mark-gerarts avatar mark-gerarts commented on June 18, 2024 1

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.

mark-gerarts avatar mark-gerarts commented on June 18, 2024

Could you provide some sample code for this? That would help a lot to figure out this issue!

from automapper-plus.

dingledorf avatar dingledorf commented on June 18, 2024

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.

mark-gerarts avatar mark-gerarts commented on June 18, 2024

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.

dingledorf avatar dingledorf commented on June 18, 2024

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.

ErikJson avatar ErikJson commented on June 18, 2024

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.

mark-gerarts avatar mark-gerarts commented on June 18, 2024

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.

wgnd93 avatar wgnd93 commented on June 18, 2024

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.

mark-gerarts avatar mark-gerarts commented on June 18, 2024

Reopening because I want to test if #21 actually fixes this issue

from automapper-plus.

r-alexandr avatar r-alexandr commented on June 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.