I am developing a EAV system for multiple entities (Product, Company,...) I will tell the problem at the end.
The database structure is the following:
EAV_ENTITY
This table will give me a list of all available Entities, like:
1 | Product
2 | Company
Structure:
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| model | varchar(255) | NO | | | |
+-------+------------------+------+-----+---------+----------------+
EAV_ATTRIBUTE
This table have a list of attributes, the ref is a mnemonic for getting the attributes by query easier. I add here the entity_type_id that is related with the table eav_entity because I want to limit each attribute to only one entity.
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| entity_type_id | int(11) unsigned | NO | MUL | NULL | |
| ref | varchar(50) | NO | UNI | | |
| name | varchar(255) | NO | UNI | | |
+----------------+------------------+------+-----+---------+----------------+
EAV_VALUE
This table have the values related to the attribute, values would be: red, green, orange, for attribute "color"
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| attribute_id | int(11) unsigned | NO | MUL | NULL | |
| value | varchar(255) | NO | MUL | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
EAV_ATTRIBUTE_VALUE
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| entity_id | int(11) unsigned | YES | | NULL | |
| entity_type_id | int(11) unsigned | YES | MUL | NULL | |
| value_id | int(11) | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
Here I make all the "magic", I make a query making a relation between an entity(type, id) and all the values related and the attribute that the value belongs to.
My query is this one
SELECT
e.model,
av.entity_id,
a.id as category_id,
a.ref as category_ref,
v.id as value_id
FROM eav_entity as e
RIGHT JOIN eav_attribute_value as av
ON e.id = av.entity_type_id
LEFT JOIN eav_value as v
ON v.id = av.value_id
LEFT JOIN eav_attribute as a
ON v.attribute_id = a.id
WHERE e.model = 'App\Product'
PROBLEM & QUESTION
When I add the statement WHERE e.model ='Whatever' I dont get any result. And it is supposed to be filter. But without the WHERE statement I get the results as expected. I think that it might be a problem related to the way I am doing the joins.
A real example without WHERE e.model
+-------------+-----------+-------------+---------------------------+----------+
| model | entity_id | category_id | category_ref | value_id |
+-------------+-----------+-------------+---------------------------+----------+
| App\Product | 13 | 2 | product_development_phase | 2 |
| App\Product | 13 | 2 | product_development_phase | 3 |
| App\Product | 13 | 4 | product_therapeutics | 58 |
| App\Company | 13 | 4 | product_therapeutics | 58 |
+-------------+-----------+-------------+---------------------------+----------+
AS @Uuerdo said, I need to add a second backslash as escape character to interpret the second one as a real backslash.
The query will result in the next below:
SELECT
e.model,
av.entity_id,
a.id as category_id,
a.ref as category_ref,
v.id as value_id
FROM eav_entity as e
RIGHT JOIN eav_attribute_value as av
ON e.id = av.entity_type_id
LEFT JOIN eav_value as v
ON v.id = av.value_id
LEFT JOIN eav_attribute as a
ON v.attribute_id = a.id
WHERE e.model = "App\\Product"