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

Rework based on #18 Normalize fields #25

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

abbadon1334
Copy link
Collaborator

As already done in #18 by @PhilippGrashoff i continue from there and i add some test to all the cases.

PhilippGrashoff and others added 6 commits May 21, 2019 00:07
This is supposed as proposal to fix atk4/data#403 going with the solution @DarkSide666 proposed.

This change routs all GET requests (single record and multiple record as well) through exportModel() function, with uses Model->export() while typecasting set to false. This way, date, time and datetime fields are not cast into PHP \DateTime objects, but the values are returned as stored in Persistence.

The solution posted here works for the mentioned date/time fields, however I see some problems:
1) what about fields that should get casting? In the end, API should return usable values. E.g about type money? API should return them as formatted to display 2 digits after the dot, shouldnt it? Is this still done?
2) getting a single record should throw an Exception when the requested record is not found. For that, it has to be loaded(). export() in exportModel() loads again, causing an unneccessary DB request.
@codecov
Copy link

codecov bot commented Apr 8, 2020

Codecov Report

Merging #25 into develop will increase coverage by 1.46%.
The diff coverage is 95.23%.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop      #25      +/-   ##
=============================================
+ Coverage      81.16%   82.63%   +1.46%     
- Complexity        76       81       +5     
=============================================
  Files              1        1              
  Lines            154      167      +13     
=============================================
+ Hits             125      138      +13     
  Misses            29       29              
Impacted Files Coverage Δ Complexity Δ
src/Api.php 82.63% <95.23%> (+1.46%) 81.00 <2.00> (+5.00)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 539545a...fdaeb26. Read the comment docs.

$data = [];
foreach ($allowed_fields as $fieldName) {
$field = $m->getField($fieldName);
$data[$field->actual ?? $fieldName] = $field->toString();
Copy link
Contributor

Choose a reason for hiding this comment

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

I dont think using actual is sensible. Think of this example Model:

$this->addField('name');
$join = $this->join('some_table');
$join->addField('other_name', ['actual' => 'name']);

That would lead to replacing the name of the model's name field with the name field from the joined table, which was sensibly declared in the model as other_name

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In the demo there was a Model with few fields with actual defined and even if i don't use it i try to fix the right field name to return back in api response

Copy link
Contributor

Choose a reason for hiding this comment

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

Thinking of it this way:
A Model can have each field name only once. So no trouble using this as array key.
When using actual property, we can have duplicate array keys, which leads to false/incomplete data passed via API.
I think actual is meant for Persistences only.

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.

3 participants