Skip to content

Nested date format is different from top level in response #2129

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

Closed
alexsmko opened this issue Oct 23, 2020 · 8 comments
Closed

Nested date format is different from top level in response #2129

alexsmko opened this issue Oct 23, 2020 · 8 comments
Labels

Comments

@alexsmko
Copy link

  • Laravel-mongodb Version: 3.7
  • PHP Version: 7.4.9

Steps to reproduce

  1. create model with propery "nested.date"
  2. return it as JsonResource for example
  3. you will date in "2020-10-23 09:12:19" format where other dates in "2020-10-23T09:12:19.512000Z"

It looks like $this->serializeDate() should be called in Mongodb/Eloquent/Model.php#L219 in order to mimic Eloquent's addDateAttributesToArray() in Eloquent/Concerns/HasAttributes.php#L168 trait

@divine
Copy link
Contributor

divine commented Oct 23, 2020

Hello,

I see that you've already debugged and found resolution, could you please push a new PR with tests?

Thanks!

@divine divine added the bug label Oct 23, 2020
@alexsmko
Copy link
Author

Will try later. Also there is another related bug: new object of such Model fails

[2020-10-23 10:50:25] production.ERROR: invalid document for insert: keys cannot contain ".": "last_request.date" {"exception":"[object] (MongoDB\\Driver\\Exception\\InvalidArgumentException(code: 22): invalid document for insert: keys cannot contain \".\": \"last_request.date\" at /src/vendor/mongodb/mongodb/src/Operation/InsertOne.php:132)

in my case last_request is null and there is no nested date, didn't dig deep into this,
added this overrides on my model as temporary solution

    // Skip setting dotted dates as attribute for new instance
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        foreach ($this->getDates() as $key) {
            if (Str::contains($key, '.')) {
                unset($this->attributes[$key]);
            }
        }
    }

    // Avoid issue with formatting nested dates
    public function attributesToArray()
    {
        $attributes = parent::attributesToArray();
        foreach ($this->getDates() as $key) {
            if (Str::contains($key, '.') && Arr::has($attributes, $key)) {
                Arr::set($attributes, $key, $this->serializeDate($this->asDateTime(Arr::get($attributes, $key))));
            }
        }
        return $attributes;
    }

@MeerKatDev
Copy link

I'm having the same issue, was it merged so far?

@divine
Copy link
Contributor

divine commented Jun 9, 2021

I'm having the same issue, was it merged so far?

No, that's the reason why this issue is still open.

Thanks.

@alexsmko
Copy link
Author

alexsmko commented Jun 9, 2021

@divine, question about local tests: readme says that all you need is to run "docker-compose up",
but it doesn't work to me w/o 'command: sh -c "composer install --no-interaction && /code/vendor/bin/phpunit"'on "tests" image in docker-compose.yml - did I miss something?

Regardless #2129 (comment) - I'm unable to reproduce this for now, will open separate issue if this happens again.

Thanks.

@divine
Copy link
Contributor

divine commented Jun 11, 2021

Hello @alexsmko ,

Yes, you're right indeed, it doesn't start the test. Docker compose file needs to be updated as well, would you like to help? 😉

Thanks!

@avivais
Copy link

avivais commented Dec 14, 2021

This is still an issue:

  1. Whenever I try to set a field that's not included in $dates array with Carbon value, it results with an empty object
  2. When I try to add nested.date field and set it to a Carbon value, it's also saved as an empty object

Any workaround? Sometthing I might be missing?

@GromNaN
Copy link
Member

GromNaN commented Sep 3, 2024

Fixed by #3105

@GromNaN GromNaN closed this as completed Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants