3

I want to add content to a repeatable component in my beforeUpdate hook. (adding a changed slug to a “previous slugs” list) in v3, I could just push new data on the component array and it would save.

in v4, it doesn’t work like that. Component data now holds __pivot: and such. I do not know how to add new data to this. I’ve tried adding a component with the entityService first, and adding that result to the array. It seemed to work, but it has strange behavior that the next saves puts in two entries. I feel like there should be an easier way to go about this.

3 Answers 3

3

You can update, create and delete component data that is attached to a record with Query Engine API, which is provided by Strapi.

To modify component data you just need the ID.

const { data } = event.params;

const newData = {
  field1: value1,
  etc...
};

await strapi.query('componentGroup.component').update({
  where: { id: data.myField.id },
  data: newData
})

When you have a component field that equals null you need to create that component and point to it.

const tempdata = await strapi.query('componentGroup.component').create(
  { data: newData }
);

data.myField = {
  id: tempdata.id,
  __pivot: {
    field: 'myField',
    component_type: 'componentGroup.component'
  }
}

Se the Strapi forum for more information.

Sign up to request clarification or add additional context in comments.

Comments

1

It seems like the way to go about this is to create the pivot manually:

// create an entry for the component
const newRedirect = await strapi.entityService.create('redirects.redirect', {
  data: {
    from: oldData.slug,
  },
});
// add the component to this model entry
data.redirects = [...data.redirects, {
  id: newRedirect.id,
  __pivot: { field: 'redirects', component_type: 'redirects.redirect' },
}];

But this feels pretty hacky. If I change the components name or the field key, this will break. I'd rather have a Strapi core way of doing this

Comments

0

the way strapi currently handles components is by providing full components array, so in case you want to inject something, you have to read components first and then apply full update, if it makes it clear.

Update

So after few hours of searching, had to do few hours of trail and error, however here is the solution, using knex:

module.exports = {
  async beforeUpdate(event) {
    // get previous slug
    const { slug: previousSlug } = await strapi.db
      .query("api::test.test")
      .findOne({ where: event.params.where });

      // create component
    const [component] = await strapi.db
      // this name of components table in database
      .connection("components_components_previous_slugs")
      .insert({ slug: previousSlug })
      .returning("id");

    // append component to event
    event.params.data.previousSlugs = [
      ...event.params.data.previousSlugs,
      {
        id: component.id,
        // the pivot, you have to copy manually
        // 'field' is the name of the components property
        // 'component_type' is internal name of component
        __pivot: {
          field: "previousSlugs",
          component_type: "components.previous-slugs",
        },
      },
    ];
  },
};

So, seems there is no service, or something exposed in strapi to create component for you.

The stuff that also required to be noted, on my first attempt i try to create relation manually in tests_components table, made for me after i added a repeatable component, to content-type, but after an hour more i found out that is WRONG and should not be done, seems strapi does that under the hood and modifying that table actually breaks logic...

so if there is more explanation needed, ping me here...

result: enter image description here enter image description here

7 Comments

Thanks for your response. Since the current data is already present on const { data } = event.params;, I don't need to fetch it. It's already at data.previousSlugs. Adding something to that array doesn't work. Mutating data. It's kind of weird to do a entityService.update in the beforeUpdate don't you think ;)
Yes, but, why would you want to add it in beforeUpdate in a first place?
so if i get your question correct, you want to add previous slug to a component array?
@LaurensKling updated answer with solution
Thanks for taking so much time for this. I'm sure there is an easier way tho. Doing manual db queries doesn't seem right. Also, you can create components thru the entityService like: strapi.entityService.create('redirects.redirect')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.