0
Good afternoon, I am developing a framework in the Mvc model and wanted to list the categories and subcategories I am using a single table CATEGORY. with the following fields.
mysql> select * from ws_categoria;
+--------------+-----------+-----------+--------------+
| id_categoria | id_parent | categoria | subcategoria |
+--------------+-----------+-----------+--------------+
| 4 | NULL | VIATUAS | NULL |
| 5 | NULL | DESPESAS | NULL |
| 6 | NULL | OUTROS | NULL |
| 7 | 4 | NULL | viatura 1 |
| 8 | 4 | NULL | viatrua 2 |
| 9 | 5 | NULL | gasolina |
| 10 | 5 | NULL | pneus |
| 11 | 6 | NULL | Reparacao |
| 12 | 6 | NULL | Pintura |
+--------------+-----------+-----------+--------------+
only that the problem and when I will try to recover the data I cannot separate the data from the type.
IN CATEGORY VEHICLES bring only the groups of vehicles as well as the other categories, the query I am using is this.
SELECT
app.categoria.categoria,
app.subcategoria.subcategoria
FROM
app.ws_categoria AS subcategoria
JOIN app.ws_categoria AS categoria
ON app.subcategoria.id_parent = app.categoria.id_categoria
RESULT
+-----------+--------------+
| categoria | subcategoria |
+-----------+--------------+
| VIATUAS | viatura 1 |
| VIATUAS | viatrua 2 |
| DESPESAS | gasolina |
| DESPESAS | pneus |
| OUTROS | Reparacao |
| OUTROS | Pintura |
+-----------+--------------+
CONCLUSION
When I try to recover the values in foreach the result I get is this:
array (size=6)
0 =>
object(stdClass)[13]
public 'categoria' => string 'VIATUAS' (length=7)
public 'subcategoria' => string 'viatura 1' (length=9)
1 =>
object(stdClass)[14]
public 'categoria' => string 'VIATUAS' (length=7)
public 'subcategoria' => string 'viatrua 2' (length=9)
2 =>
object(stdClass)[15]
public 'categoria' => string 'DESPESAS' (length=8)
public 'subcategoria' => string 'gasolina' (length=8)
3 =>
object(stdClass)[16]
public 'categoria' => string 'DESPESAS' (length=8)
public 'subcategoria' => string 'pneus' (length=5)
4 =>
object(stdClass)[17]
public 'categoria' => string 'OUTROS' (length=6)
public 'subcategoria' => string 'Reparacao' (length=9)
5 =>
object(stdClass)[18]
public 'categoria' => string 'OUTROS' (length=6)
public 'subcategoria' => string 'Pintura' (length=7)
I may be wrong and have not understood your question, but I believe that you are performing the database relationship incorrectly, you should create a table for CATEGORY and another for SUBCATEGORY, because in my grant the CATEGORY may have several SUBCATEGORY, then you should relate the SUBCATEGORY table to the CATEGORY table, that is, each registered category you should inform a unique category.
– Nicola Bogar
Thanks friend for the tip. :)
– Philipe Silva
Do you know how to do or would like an example?
– Nicola Bogar
create a table categories and subcategories and in the table subcategories put the table id categories in the table subcategories, and name the field in a simple way like this => id_subcategory and ai yes relate through category.id = subcategory.id_category
– Victor
Thank you friend thanks for example. I thought I would give to make 1:1 table relationships with herself. XD
– Philipe Silva
actually N subcategories may belong to 1 category, already 1 category may not belong to N subcategories, so it is CATEGORY 1 to N SUBCATEGORIES
– Victor