Comments (10)
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.
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.
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.
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.
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.
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.
Any new about this?
from automapper-plus.
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.
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.
Thanks for looking into it!
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.