-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/mapping fields transformer #20
Feature/mapping fields transformer #20
Conversation
This runs with MySQL but still needs the following fix #19 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks promising, thanks!
$result = []; | ||
foreach ($data as $key => $val) { | ||
if (array_key_exists($key, $this->fields)) { | ||
$result[$this->fields[$key]] = $val; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this transformer, but we can make it better by using https://symfony.com/doc/current/components/property_access.html and make it work with multi-dimensional arrays and objects. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me have a look. Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello there,
I believe those two may be different examples since I cannot see how the PropertyAccess
can be used to solve this issue.
I suggested a MapFieldsTransformer
to solve the specific need explained at #18 -- perhaps the name of this transformer is not too self-explanatory at this moment -- and also because it'll be good to have a bunch of transformers performing common T tasks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This:
if (array_key_exists($key, $this->fields)) {
$result[$this->fields[$key]] = $val;
Becomes:
$propertyAccessor->setValue($result, $this->fields[$key], $val);
Advantage? It can set arrays, objects, private fields if there are setters... It's more powerful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks for the remark. The file is been updated.
- The
PropertyAccess
component is used - The name of this transformer remains the same:
MapFieldsTransformer
$propertyAccessor = PropertyAccess::createPropertyAccessor(); | ||
|
||
foreach ($data as $key => $val) { | ||
if (array_key_exists($key, $this->fields)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$propertyAccessor->isWritable($result, "[{$this->fields[$key]}]")
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one:
foreach ($data as $key => $val) {
if ($propertyAccessor->isWritable($result, "[{$this->fields[$key]}]")) {
$propertyAccessor->setValue($result, "[{$this->fields[$key]}]", $val);
}
}
[07-May-2018 20:29:54 Europe/London] PHP Notice: Undefined index: isbn in /home/standard/projects/Extraload/src/Extraload/Transformer/MapFieldsTransformer.php on line 23
[07-May-2018 20:29:54 Europe/London] PHP Fatal error: Uncaught Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException: Could not parse property path "[]". Unexpected token "[" at position 0 in /home/standard/projects/Extraload/vendor/symfony/property-access/PropertyPath.php:116
Stack trace:
#0 /home/standard/projects/Extraload/vendor/symfony/property-access/PropertyAccessor.php(886): Symfony\Component\PropertyAccess\PropertyPath->__construct('[]')
#1 /home/standard/projects/Extraload/vendor/symfony/property-access/PropertyAccessor.php(297): Symfony\Component\PropertyAccess\PropertyAccessor->getPropertyPath('[]')
#2 /home/standard/projects/Extraload/src/Extraload/Transformer/MapFieldsTransformer.php(23): Symfony\Component\PropertyAccess\PropertyAccessor->isWritable(Array, '[]')
#3 /home/standard/projects/Extraload/src/Extraload/Pipeline/DefaultPipeline.php(38): Extraload\Transformer\MapFieldsTransformer->transform(Array)
#4 /home/standard/projects/Extraload/examples/doctrine/04_default_doctrine_query_map_fields_transformer_dbal_loader.php(21): Ex in /home/standard/projects/Extraload/vendor/symfony/property-access/PropertyPath.php on line 116
The following one:
foreach ($data as $key => $val) {
if ($propertyAccessor->isWritable($result, $this->fields[$key])) {
$propertyAccessor->setValue($result, "[{$this->fields[$key]}]", $val);
}
}
[07-May-2018 20:32:04 Europe/London] PHP Notice: Undefined index: isbn in /home/standard/projects/Extraload/src/Extraload/Transformer/MapFieldsTransformer.php on line 23
[07-May-2018 20:32:04 Europe/London] PHP Fatal error: Uncaught Symfony\Component\PropertyAccess\Exception\InvalidArgumentException: The property path constructor needs a string or an instance of "Symfony\Component\PropertyAccess\PropertyPath". Got: "NULL" in /home/standard/projects/Extraload/vendor/symfony/property-access/PropertyPath.php:80
Stack trace:
#0 /home/standard/projects/Extraload/vendor/symfony/property-access/PropertyAccessor.php(886): Symfony\Component\PropertyAccess\PropertyPath->__construct(NULL)
#1 /home/standard/projects/Extraload/vendor/symfony/property-access/PropertyAccessor.php(297): Symfony\Component\PropertyAccess\PropertyAccessor->getPropertyPath(NULL)
#2 /home/standard/projects/Extraload/src/Extraload/Transformer/MapFieldsTransformer.php(23): Symfony\Component\PropertyAccess\PropertyAccessor->isWritable(Array, NULL)
#3 /home/standard/projects/Extraload/src/Extraload/Pipeline/DefaultPipeline.php(38): Extraload\Transformer\MapFieldsTransformer->transform(Array)
#4 /home/standard/projects/Extraload/examples/doctrine/04_default_doctrine_quer in /home/standard/projects/Extraload/vendor/symfony/property-access/PropertyPath.php on line 80
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmm. Perhaps the PropertyAccess
may not be necessary in this particular mapper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me debug, what php script are you running?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am running examples/doctrine/04_default_doctrine_query_map_fields_transformer_dbal_loader.php
with PHP 7.1.17
Here is the
MapFieldsTransformer
as discussed at #18