Git Product home page Git Product logo

Comments (10)

mark-gerarts avatar mark-gerarts commented on June 15, 2024 2

It is now possible to ignore null properties on the source object by using $options->ignoreNullProperties(). This is available on master, and will be part of the next release.

from automapper-plus.

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

Hi there,

I want to make sure I understand the situation correctly. Let's say you have this setup:

<?php

class A
{
    public $description;
}

class B
{
    public $description;
}


$config = new AutoMapperConfig();
$config->registerMapping(A::class, B::class);
$mapper = new AutoMapper($config);

$a = new A();
$b = new B();

Am I correct that you want to achieve the following:

<?php

$b->description = 'some value';

$a->description = '';
$b = $mapper->mapToObject($a, $b);
// $b->description is now the empty string. This is okay.

$a->description = null;
$b = $mapper->mapToObject($a, $b);
// $b is now overwritten to be `null`. This is something you don't want -
// if the source property is null, you want to ignore it. 

from automapper-plus.

stesvis avatar stesvis commented on June 15, 2024

Thanks for your prompt reply.
Yes that would be the ideal scenario.

Right now in both cases after the mapping b->description is always 'some value'.

It seems that the mapping is ignored unless a->description is not empty and not null.

from automapper-plus.

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

Alright, I'll try to look into this the following days/weeks. I'm either gonna provide an option to achieve this behaviour, or implement a new operation.

One small thing though, which version are you using? I tested the sample code using the latest version, and there $b->description would always be overwritten, instead of it remaining 'some value'. This seems a bit weird to me, unless you're using a slightly different configuration?

from automapper-plus.

stesvis avatar stesvis commented on June 15, 2024

Thanks,
this is what I have right now:
"mark-gerarts/auto-mapper-plus": "^1.1",

I have one more question:

Model: Guide has some relationships

class Guide extends Model
{
   // ...

   public function bikes()
   {
       return Bike::whereIn("id", $this->stable()
           ->select("bike_id")
           ->get()
           ->toArray());
   }

   public function tours()
   {
       return $this->hasMany(Tour::class);
   }
}

DTO: GuideDTO has the matching properties

class GuideDTO
{
    // ...

    public $bikes;
    public $tours;
}

Mapper config

$config->registerMapping(Guide::class, GuideDTO::class)->reverseMap();
$config->registerMapping(Bike::class, BikeDTO::class)->reverseMap();

ISSUE

$mapper->map($guide, GuideDTO::class);

bikes: "App\Models\Guide::bikes must return a relationship instance."
tours: is properly mapped

NOTE: If i do this, it works fine:

$bikeDTOs = $mapper->mapMultiple($guide->bikes()->get(), BikeDTO::class);
$guideDTO = $mapper->map($guide, GuideDTO::class);
$guideDTO->bikes = $bikeDTOs;

Is there a solution to map everything in one shot with just this?
$guideDTO = $mapper->map($guide, GuideDTO::class);

from automapper-plus.

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

Thanks for the detailed write-up! This'll definitely help with debugging. Unfortunately, I don't have much time to take a look at this issue at this moment. I'll try to get back to you next week!

from automapper-plus.

Koronos avatar Koronos commented on June 15, 2024

Any new about this?

from automapper-plus.

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

I have to admit I kinda lost this one out of sight. I can't make any promises when this will be resolved as I'm a bit busy the following weeks. Anyway:

  • The issue might have been resolved already due to a previous fixed issue involving Doctrine. The first step would be to check this.
  • The feature request is still open to implement. I'll look into this after the first issue has been resolved.

from automapper-plus.

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

I've had some time to review your issue @stesvis. It seems to be that Eloquent's __isset method checks if a property could represent a relationship. Since your bikes method doesn't actually return a relationship, but a query result, an exception gets thrown. Returning for example $this->hasMany(...) instead in the bikes method, results into a mapped DTO. However, this'll likely change the behaviour you want, so your temporary solutions seems to me the best fix for this.

from automapper-plus.

stesvis avatar stesvis commented on June 15, 2024

Thanks for looking into it!

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.