M
M
MaikMain2017-06-05 12:42:38
Yii
MaikMain, 2017-06-05 12:42:38

How to make a selection for several categories at once. Yii2?

I have a two level menu, the categories are related by id and parent_id in the CATEGORY table.
Products in a particular category are displayed by category_id in the CATEGORY table.
I need to display products in the parent category from all child ones.
Tell me how can this be done? Or maybe someone will lead somewhere.
Thank you very much in advance)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-06-05
@qonand

if you have a maximum nesting level of 2 (i.e. category and subcategory) then you can select products from the parent category with something like this query:

SELECT
  *
FROM
  product
WHERE
  category_id IN (
    SELECT
      id
    FROM
      category
    WHERE
      parent_id = 2
  )

where 2 is the ID of the parent category.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question