Skip to content

toArray() & fill() issue with dates #1731

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

Open
zlanich opened this issue Mar 16, 2019 · 0 comments
Open

toArray() & fill() issue with dates #1731

zlanich opened this issue Mar 16, 2019 · 0 comments

Comments

@zlanich
Copy link

zlanich commented Mar 16, 2019

I noticed when I initially synced all my models over to Mongo, that I accidentally set the dates as strings. When I went to re-sync, I noticed an odd issue with converting dates.

I have a simple sync job that takes an update action, pulls all the existing values from my MySql model, finds the existing Mongo model, then copies the attributes over. Here's what it looks like:

Context: ->toMoloquent() just creates a Moloquent model instance from my MySql model (different att schemas)

$existing = MyMoloquentModelClass::where('id', $mysqlModel->id)->first();
$updatedModel = $mysqlModel->toMoloquent();
$existing->fill($updatedModel->toArray());

When I do this, if date attributes were previously saved to Mongo as strings, it will not convert them to Mongo BSON dates. It leaves them as strings. I have to do this to make it work:

foreach ($existing->getDates() as $date)
{
    $existing->{$date} = $updatedModel->{$date};
}
$existing->save();

I have my protected $dates attribute set up correctly on my model, and it works fine when creating a fresh model, and if the dates were saved properly in the first place. I've also confirmed that $updatedModel's date attributes are indeed Mongo BSON dates that I'm passing into ->fill().

Help!

@divine divine mentioned this issue Mar 1, 2020
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

No branches or pull requests

1 participant