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

Issue #2935: `createOrFirst` is not triggering `creating` nor `created` model events

    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      prwnr has created Issue #2935: `createOrFirst` is not triggering `creating` nor `created` model events in laravel-mongodb. This Jira ticket was filed by GromNaN

      Issue Text:

      • Laravel-mongodb Version: 4.3.0
      • Laravel version: 11.6.0
      • PHP Version: 8.2.2

      Description:

      There seem to be a problem, where using the `createOrFirst` builder method, that it does not trigger the `creating` or `created` model events when the model is being created (not retrieved, as there is no matching model found).

      This seems to be caused by the fact that the `createOrFirst` method is overwritten in MongoDB package, but it is not calling the `$model->save()` method after an attempt to find the model, which would trigger those two events.

      This is how it behaved in the past (I just upgraded from v3 to v4) and it is how it behaves in Laravel without MongoDB.

      Steps to reproduce

      1. set up a simple Model class
      2. call `Model::query()->createOrFirst()` on it
      3. see that it won't trigger the `creating` and `created` event if it recently created the model (there was no matching model for the attributes)

      This will however trigger the `retrieving` event instead - even if the model was recently created, but not actually "retrieved"

      Expected behaviour

      When a model is being created as a new one with use of `createOrFirst` method, it should trigger the `creating` and `created` model events.

      Actual behaviour

      When a model is being created as a new one with use of `createOrFirst` method, there is no `creating` nor `created` events dispatched.

      Workaround

      At the moment the only workaround I could think of is to manually try to fetch the model first with use of find/where query, and if its not found, create it with use of `Model::query()>create()` method, or `$model>save()` - which both will dispatch the `creating` and `created` events.

            Assignee:
            jerome.tamarelle@mongodb.com Jérôme Tamarelle
            Reporter:
            dbeng-pm-bot PM Bot
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: