Skip to content
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

Conversation

programarivm
Copy link
Contributor

Here is the MapFieldsTransformer as discussed at #18

@programarivm
Copy link
Contributor Author

programarivm commented Apr 28, 2018

This runs with MySQL but still needs the following fix #19

Copy link
Owner

@umpirsky umpirsky left a 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;
Copy link
Owner

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?

Copy link
Contributor Author

@programarivm programarivm May 3, 2018

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.

Copy link
Contributor Author

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.

Copy link
Owner

@umpirsky umpirsky May 7, 2018

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.

Copy link
Contributor Author

@programarivm programarivm May 7, 2018

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)) {
Copy link
Owner

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]}]").

Copy link
Contributor Author

@programarivm programarivm May 7, 2018

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

Copy link
Contributor Author

@programarivm programarivm May 7, 2018

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.

Copy link
Owner

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?

Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants