Uploaded image for project: 'PHP ORMs'
  1. PHP ORMs
  2. PHPORM-309

laravel-mongodb - Issue #3326: saveQuietly() fails inside created event listener

    • Type: Icon: Question Question
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Laravel
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      andresantos-tech has created Issue #3326: saveQuietly() fails inside created event listener in laravel-mongodb. This Jira ticket was filed by GromNaN

      Issue Text:

      Description:

      I'm having an issue using this package with models that have a "created" event listener.
      I've created a minimal repository with a clean Laravel 10 installation where the error occurs:
      🔗 [GitHub Repo](https://github.com/andresantos-tech/laravel10-mongodb-PoC)

      I have the following in my model:

      static::created(function (MyModelWithCreatedEventNotWorking $model) {
          $model->bla = 'bla';
          $model->saveQuietly();
      });
      

      However, when saving the model, I get the following error:

      InvalidArgumentException  Cannot update "id" field
      

      This exception is thrown in Builder.php#L742(https://github.com/mongodb/laravel-mongodb/blob/5.1.1/src/Query/Builder.php#L742).

      #h1. Example
      This code triggers the error:

      $model = new \App\Models\MyModelWithCreatedEventNotWorking();
      $model->value = 'value';
      $model->save();
      // InvalidArgumentException  Cannot update "id" field
      

      However, if I call saveQuietly() *outside* the static::created event, it works as expected:

      $model = new \App\Models\MyModelWithoutCreatedEvent();
      $model->value = 'value';
      $model->bla = 'bla';
      $model->saveQuietly();
      // Works fine
      

      #h1. Workaround
      To make it work inside the static::created event, I need to call refresh() before saving:

      static::created(function (MyModelWithCreatedEventWorking $model) {
          // Fix
          $model->refresh();
          $model->bla = 'bla';
          $model->saveQuietly();
      });
      

      Now, the model saves without issues:

      $model = new \App\Models\MyModelWithCreatedEventWorking();
      $model->value = 'value';
      $model->save();
      // Works fine
      

      This behavior seems to be a bug because it worked correctly in Laravel 9.* with "jenssegers/mongodb": "^3.9".

      Steps to reproduce

      1. Clone the repo 🔗 [GitHub Repo](https://github.com/andresantos-tech/laravel10-mongodb-PoC)
      2. Install dependencies
      3. Run the examples above in `artisan tinker`

      Expected behaviour

      >saveQuietly() inside the static::created event should work without needing to call $model>refresh().

      Actual behaviour

      >saveQuietly() inside the static::created event only works after calling $model>refresh(). Without refreshing, it throws the exception:
      InvalidArgumentException Cannot update "id" field.

      Thanks! 😊

            Assignee:
            Unassigned Unassigned
            Reporter:
            dbeng-pm-bot TPM Jira Automations Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              None
              None
              None
              None