1

I have two tables in postgresql in strapi: goods and labels

Goods:
id | title
---------
1 | some name
2 | some name 2
3 | some name 4
…………………

Labels
id | title
------------
1 | some label 1
2 | some label 2
3 | some label n
………………………

They are connected with each other many to many, through an additional table

goods_labels__labels_goods
id | good_id      | label_id
----------------------------
1  |  1           | 1
2  |  1           | 2
2  |  2           | 1

I need to select goods that have a relationship with both label.id = 1 and label.id = 2, this is only the first product with id = 1. Is any way to implement it with standart strapi tools with graphql or with bookshelf, or as a last resort through a raw query? Also I need to sort and limit this request

2
  • Have you tried _in filter? In graphql something like { goods(where: {label_in: [1,2]} )} Commented Jan 17, 2022 at 14:34
  • Did you ever figure this out? Commented Feb 10, 2022 at 2:40

3 Answers 3

1

ok. the content is here.

I have two content-type in strapi like tag and article. And want to filter articles which both have several tags like 1,2,4. In the standard strapi, you cannot do this .so I have to change the default behavior of strapi article api. you can find the document in here

first

we need to define the special parameter to reach our goals. in fact I choose the multiTag in the query parameter

query{
    multiTag: [1,2,3]   
}

second

open file in your project

/src/api/article/controllers/article.js

! notice: the path would change with your content-type name.

you should get this

const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::article.article');

then add some code to this

async function getMultiTag(conn, multiTag){
    // the sql should like this 
    const sql = `
        select 
            t.id 
        from 
            article t 
        inner join (
            select article_id from article_link_tag 
            where tag_id in (${string(multiTag})
            group by article_id having count(*) = ${multiTag.length}
        )t2 on t.id = t2.article_id
    `
    const result = await conn.raw(sql)
    // result should like this 

[
    // actual data
    [
        {"id" : 1}  
    ],
  // meta info
    [
    {
        "db" : 1
    }
    ]
]

    return result[0]
}

module.exports = createCoreController('api::pet-article.pet-article', ({ strapi }) => ({
   async find(ctx) {
    ctx.query = { ...ctx.query, local: 'en' }
        // get the param
    const multiTag = ctx.query?.multiTag
    if (multiTag && typeof multiTag == 'object' && multiTag.length > 0) {
            // get the database connection
      const conn = strapi.db.connection
      const articles = await getMultiTag(conn, multiTag)
            // when you have got the ids of article 
            // you can do many things
            // like this 
            ctx.query.filter = {
                ...(ctx.query.filter || {}),
                id: {
                    $in: articles.map(val => val.id)
                }
            }
            // or just return the ids
            // return { data: articles }
    }
    const { data, meta } = await super.find(ctx);
    meta.date = Date.now()
    return { data, meta };
  },
}));
Sign up to request clarification or add additional context in comments.

Comments

0

I haven't tested it, but based on the docs, try this:

const hasAnyLabel = {
  labels: {
    id: {
      $in: [1,2]
    }
  }
};

const hasAllLabels = {
  labels: {
    $and: [
      {id: 1},
      {id: 2},
    ]
  }
};

const goods = await strapi.db.query('api::goods.goods').findMany({
  where: hasAllLabels,
  populate: ['labels']
});

Comments

0

In Postman (for those using filters in the URL), for me, this has worked:

localhost:8080/api/blogs?populate=*&filters[categories][uid][$eq]=react

I have a blogs content-type and a blog-categories content-type with a many-to-many relationship. A blog can have several categories, and a category can have several posts. So, if I want to filter blogs that belong to a specific category, I do it as I described above. 👆

Using the strapi client:

const { data } = await strapi.find('blogs', {
  populate: 'deep',
  filters: { categories: { uid: { $contains: 'category-name' } } },
  pagination: { page: 1, pageSize: 3 },
});

Comments

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.