mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Home Page:https://www.mongodb.com/compatibility/mongodb-laravel-integration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

function addUpdatedAtColumn use array_merge bug

holla-renzhenguo opened this issue · comments

  • Laravel-mongodb Version: 3.8,5
  • PHP Version: 8.0
  • Database Driver & Version:

Description:

addUpdatedAtColumn 方法使用了array_merge来合并数组,在使用数值键的情况下会丢失键

The addUpdatedAtColumn method uses array_merge to merge arrays, which can result in key loss if numeric keys are used.

Steps to reproduce

$test = new TestModel();
$test->user_id = 1;
$test->{2} = "a";
$test->{4} = "b";
$test->save();
2.
$test = TestModel::where('user_id', 1)->first();
$test->{2} = "c";
$test->save();

  1. Database
    {
    _id: ****,
    user_id: 1,
    0: "c",
    2: "a",
    4 : "b",
    updated_at: ****,
    created_at: ****
    }

Expected behaviour

Tell us what should happen

Actual behaviour

Tell us what happens instead

Logs: Insert log.txt here (if necessary)

Hello @holla-renzhenguo,
This bug needs to be reported to laravel/framework as the Eloquent Builder has the same issue if you use a numeric field name. We try to stick to the parent Eloquent implementation to be able to update according to changes in the framework.

They the also uses array_merge everywhere to merge the $attribute array with other data from casting, default values and so on...

We discourage you from using integer field names, even if MongoDB server support them you will find edge cases that need to be fixed in Eloquent. You can try to submit a PR on laravel/framework to change array_merge with array_replace, but it's up to the laravel maintainers to accept it or not.