We use cookies to help make our site work properly and to analyze how our site is used. Some are optional, but none contain your personal information, and we don't use any for ads. You can view our cookies policy, manage your cookie preferences, or consent and dismiss this banner by clicking agree:

Bug #23565
See Comments

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

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')-&gt;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.

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?

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.

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.

Username

Password

Email Address

Used to log in

Display Name

Password

By registering, you agree to our terms of service, including receiving some tips and offers from us from time to time. We never spam, and we never share your email address with third parties. Terms of ServicePrivacy Policy