laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

Nested date format is different from top level in response

Open alexsmko opened this issue 3 years ago • 7 comments

  • 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

alexsmko avatar Oct 23 '20 09:10 alexsmko

Hello,

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

Thanks!

divine avatar Oct 23 '20 10:10 divine

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;
    }

alexsmko avatar Oct 23 '20 11:10 alexsmko

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

MeerKatDev avatar Jun 09 '21 10:06 MeerKatDev

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

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

Thanks.

divine avatar Jun 09 '21 11:06 divine

@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 https://github.com/jenssegers/laravel-mongodb/issues/2129#issuecomment-715281885 - I'm unable to reproduce this for now, will open separate issue if this happens again.

Thanks.

alexsmko avatar Jun 09 '21 19:06 alexsmko

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!

divine avatar Jun 11 '21 01:06 divine

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?

avivais avatar Dec 14 '21 17:12 avivais