Comments (7)
How would you configure it?
from form.
We will configure some mapper/automapper in container by giving it mappings. If the names of the properties are the same it would be enough and it will allow also extended configuration.
Example from other library (there are multiple php mappers, the basic one will be built-in to the package) :
$config = new AutoMapperConfig();
// Simply registering the mapping is enough to convert properties with the same
// name. Custom actions can be registered for each individual property.
$config
->registerMapping(Employee::class, EmployeeDto::class)
->forMember('age', function (Employee $source) {
return date('Y') - $source->getBirthYear();
})
->reverseMap(); // Register the reverse mapping as well.
$mapper = new AutoMapper($config);
// With this configuration we can start converting our objects.
$john = new Employee(10, "John", "Doe", 1980);
$dto = $mapper->map($john, EmployeeDto::class);
echo $dto->firstName; // => "John"
echo $dto->lastName; // => "Doe"
echo $dto->age; // => 37
from form.
Can't you, instead, add the following to your custom form model:
final class PersonForm extends FormModel
{
// usual form stuff
public function getPerson(): Person
{
$person = new Person();
$person->firstName = $this->firstName;
$person->lastName = $this->lastName;
$person->age = date('Y') - $this->birthYear;
return $person;
}
}
That's only 6 lines compared to about 10 lines of configuring a mapper...
from form.
@samdark First of all you will also need the reverse one, so your code example will be larger. Second I am always recommending to do something you mentioned when just starting the project and you are not sure how big your entity and models will grow, how complex they will become, and how many of the you will have. But as practice shows it is a way more convenient to use some sort of automapper for projects with a lot of entity -> model mappings. And in case if you do the same thing with reverse and for a few model -> entity mappings, your code will be to long compared to configuring just automapper (take into account that you initialize mapper just ones and then configure only mappings, which will be just one line for simple models). I'm just proposing to have simple mapper built-in to the package (that will work flawlessly with our model) and not recommending to always use it by default.
If you think that the mapper is out of context of this package, we can simply document the possibility of using one of the mappers already available.
from form.
Alright. I forgot about reverse mapping. It makes sense overall. Let's plan it in detail before implementing.
from form.
Also mapper will help with nested forms (the implementation of nested forms #43). The code becomes very complicated when doing it manually in these cases.
from form.
This can be related:
from form.
Related Issues (20)
- Wrong html input pattern for Url::rule HOT 11
- Allow null value for input id and label for attributes
- Widgets should not overwrite explicitly set options
- URL rule regex does not work in HTML field when IDN is enabled
- Use friendly exception for exception "Form model is not set."
- Implement template for each widget from factory. HOT 3
- Change FormModel::attributes as protected or add method like getAttributeNames HOT 6
- Use array_key_exists instead of isset on check value
- Adapt after removing result set in validator
- Rename FormErrors to FormErrorSet (and similar) - consistency, best practices HOT 1
- Nested::rule and nested forms validation fails
- Make use of PHP 8 attributes for labels, hints, etc.
- Nested rule - combine dot notation and nested structure
- Yiisoft\Form\Widget\Validator\FieldValidator::getValidatorAttributes() shouldn't apply required attribute for Required rule indiscriminately
- Remove filter of null values in methods for setting classes HOT 1
- Code style - chain calls HOT 15
- Add `$content` parameter to `Field::*Button()` methods
- Yiisoft\Validator\DataSet\AttributeDataSet not found
- Dependency update/adjust to yiisoft/html HOT 2
- Nested array form with translations HOT 3
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 form.