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

updateOrCreate does not create DB entries

CaNCaNCode opened this issue · comments

  • Laravel-mongodb Version: 4.2
  • PHP Version: 8.2
  • Database Driver & Version: latest

Hello,

since Version 4.2 updateOrCreate of a Model does not work properly.
It does not create Entries in the Database.

On Version 4.1.2 was everything fine.

Reproduction:
Call updateOrCreate (with a dummy _id value, to call the create)

Thanks for the report. We tracked this bug in Jira PHPORM-172 and I'll work on it quickily.

I've just tested this unit test that I created from your "reproducer" description. It works and the data is correctly saved.

use MongoDB\Laravel\Tests\Models\User;

$user = User::updateOrCreate(
    ['_id' => 'foo'],
    ['email' => 'john.doe@example.com'],
);

$this->assertInstanceOf(User::class, $user);

The medthod updateOrCreate is inherited from Laravel (see Illuminate\Database\Eloquent\Builder::updateOrCreate)

I can't confirm the issue. Could you write a code examples that shows the issue?

updateOrCreate(['_id' => 'NOT-EXISTENT'], $data) if it is not existent I get the first entry of document back but it is not creating a new one.

Btw: Updating a given _id works perfect.

@CaNCaNCode did you make all fields fillable ( in your case "_id" )

@GromNaN created_at and updated_at are not created in the case of a new record

@CaNCaNCode did you make all fields fillable ( in your case "_id" )

Thanks for checking. It seems that in my test #2906 that even non-fillable fields are saved.

@GromNaN created_at and updated_at are not created in the case of a new record

Timestamps issue will be fixed by #2905

Thanks for checking. It seems that in my test #2906 that even non-fillable fields are saved.

look like you are only using _id to search for existing records. if you use a different field like "eyecolor" which is not fillable, it will return the first record for some reason. Because the search fields also get updated it should be fillable.

Timestamps issue will be fixed by #2905

This does not seem to work

if use the laravel function "createOrFirst" it wil get dates, but if i use the "createOrFirst" in the laravel-mongodb Builder.php, it does not.

@ithuis
Yes, the problem does not exists on 4.1.2 . On 4.1.2 everything works like a charm. So the fillable prop should be ok. There must be anything else changed. It could be releated to the date problem, because there are a lot of dependencies on created_at on my model

Please, provide a reproducer in the form of a small project that shows the issue. I can't reproduce the errors you describe. Eloquent\Builder::createOrFirst have been reimplemented in the MongoDB way in this PR: #2742.

Please, provide a reproducer in the form of a small project that shows the issue. I can't reproduce the errors you describe.

Thanks for your help, I will investigate it.