Bug #23565 See Comments

Getting entry in after_channel_entry_save hook from DB gives old values for new fields

Version: 4.1.3 Reporter: Low

For Low Search, I use the after_channel_entry_save hook to keep the search index up to date. For this, I use a method (that I use in multiple places) that takes an entry ID, retrieves the entry and updates the index accordingly. I use ee('Model')->get('ChannelEntry')... to get the entry.

In this particular hook, I get the new values for any field that is either native (like title) or ‘old’ (ie. pre EE4, still residing in exp_channel_data). But for ‘new’ fields, so fields having their own table, I get the old value and the new value isn’t actually updated in the DB yet. The ChannelEntry model object I get from EE therefore has mixed old and new values.

In this particular case, I can’t use the model that’s been passed through as an argument in the after_channel_entry_save hook, because I’m using the reusable method described above.

  • The code that updates the database fires during the onAfterInsert and onAfterUpdate events, which both fire after the onAfterSave event. Try using theafter_channel_entry_update and after_channel_entry_save hooks instead and see if that resolves it.

    Seth Barber
    17th April, 2018 at 6:22pm
  • Shouldn’t the after_channel_entry_save hook fire after creation and update? In EE3 it worked as expected, it’s just with the new DB schema that it gives me half-updated results. Also, if I use both the after_channel_entry_update and the theafter_channel_entry_save hooks, won’t each get called when a single entry gets updated?

    Low
    18th April, 2018 at 12:59pm
  • Ooops. I made a typo. I meant for you to use both *_update and *_insert hooks. The after_channel_entry_save hook has been firing before the insert and update events in v3. With updates the event order is before update, before save, after save, after update. I’ve checked and it is that way in v3 and v4, the difference here is that we are saving some values to the DB in the afterUpdate process, which does happen after the actual save() update query, and the after save event.

    Seth Barber
    18th April, 2018 at 1:48pm
  • In EE3, retrieving the entry from the DB in _save got me the updated data. In EE4, retrieving the entry using the Model gets me mixed new and old data. I recon the latter is not desirable at all.

    Also, the docs say:

    > Called after the channel entry object is inserted or updated.

    …which leads me to believe the DB should be up to date with the new data at that point.

    Low
    19th April, 2018 at 3:58am
  • You should be able to get this all working with the update and insert hooks today. We’ll take a look and see about improving the timing of the custom field to db action after we finish work on GDRP, which has a time clock.

    Seth Barber
    19th April, 2018 at 11:28am

You must be signed in to comment on a bug report.

ExpressionEngine News

#eecms, #events, #releases