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

Codeigniter\Entity - Fix ToArray datamap #3508

Merged
merged 2 commits into from
Aug 25, 2020

Conversation

najdanovicivan
Copy link
Contributor

Description

There are couple of issues with the way datamap works

  • It keeps doubles the values original and mapped where only mapped should be kept
  • If datamap is used on existing attribute it will not work properly as value is already processed through data map in the getter
  • When unsetting the key we need to double check if there is not another map that uses the key in $to map as if unset it won't be accessible in the next loop

I've added the getSwappedEntity() method which returns the entity which is affected by all those issues.

Checklist:

  • Securely signed commits
  • Component(s) with PHPdocs
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Additional information regarding this commit is provided in PR
@najdanovicivan
Copy link
Contributor Author

This PR is not final yet. This is just to understand the issue. I'm thinking about more elegant solution to avoid having all those checks and do everything in single foreach loop.

As datamap already works on magic getter the idea is to properly prepare keys and then run foreach loop to get values using those keys.

The way to prepare those keys will be done like this

  1. get all keys of $this->attributes let those be named $keys
  2. filter out keys starting with _underscore from $keys
  3. get unique values fro the $this->datamap let those be named as $mapped_keys
  4. filter out the $mapped_keys from the $key
  5. Append all keys from $this->datamap to $keys
  6. Run single foreach $keys as $key and pouplate $results with $this->__get($key)

Let me know what you think about doing it this way

@najdanovicivan
Copy link
Contributor Author

I've submitted another commit which will handle this by processing keys first

@michalsn
Copy link
Member

I like this. Nice and clean solution.

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much cleaner! Elegant solution. I think you've recreated an existing function, but let me know if I've misunderstood that.

system/Entity.php Show resolved Hide resolved
@MGatner MGatner merged commit dfbab73 into codeigniter4:develop Aug 25, 2020
@najdanovicivan najdanovicivan deleted the entity-asArray-cast branch November 16, 2020 19:42
protected $datamap = [
'bar' => 'foo',
'foo' => 'bar',
'original_bar' => 'bar',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@najdanovicivan
Each property (array key) in the Entity has a value, so if the column bar has two properties,
we need to discard one value when inserting to database.

The $datamap seems to be invalid. Am I something wrong?

@kenjis
Copy link
Member

kenjis commented Dec 24, 2021

@najdanovicivan

I've added the getSwappedEntity() method which returns the entity which is affected by all those issues.

You added the swapped case, but do you really need the use case?
It seems it gives users a lot of complexity.

If we prohibit it, the Entity would be simpler.

@kenjis kenjis mentioned this pull request Dec 24, 2021
5 tasks
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.

4 participants