Triggering lifecycle hook between non-related content types

Hey @jasonleow,

I’m glad I can make you happy, because you are indeed wrong on that :wink:.

You are free to do what every logic you want inside the ‘trigger’, as the examples in the docs try to displays. You can simply use the (core) controllers/services to edit any content type.
For example your ./api/post/models/post.js could look something like this:

'use strict';

module.exports = {

  lifecycles: {
    async afterCreate(result, data) {
      const id = result.streakID;
      const streak = strapi.services.streaks.findOne({ id });

      strapi.services.streaks.update({ id }, { counter: streak.counter++ });
    },
  },
};

I hope this helps. :+1:

Just one disclaimer: you can’t get the request context (ctx) inside the lifecycle hook.

EDIT:
Ooh and I didn’t test this, but something like this should work :wink:

1 Like