Yii beginner here. I am facing some problem with how to query the database with the scenario I have. Here is what my two tables look like. The reason category is maintained in a separate table is because a product can belong to multiple categories.
Product Table
-------------
id
product_name
product_desc
product_color
product_price
Category Table
--------------
id
product_category
product_id
A product can belong to multiple category.
Now, let's say I want to find the products of category 'xyz' with color 'blue'. I am not sure how do I query both the tables using two different models (or not) to achieve this. Any help?
Here you have many to many relationship where one product can belong to multiple categories and one category can belong to multiple products.
You will definitely need a third table
Product Table
-------------
id
product_name
product_desc
product_color
product_price
Category Table
--------------
id
category_name
Product_Category Table
----------------------
product_category_id
product_id ( foreign key to Product->id )
category_id (foreign key to Category->id )
You can implement simple query without having a model like this:
$connection=Yii::app()->db;
$sql = "SELECT .....";
$command = $connection->createCommand($sql);
$dataReader=$command->query();
$rows=$dataReader->read();
print_r($rows);
Arfeen's solution is perfectly valid.
Although, I would assume that you have already read the official Relational Active Record documentation on yii website.
Related
I'm an absolute beginner to PHP and MySQL. I have a pre-coded project which I currently making personalised modifications.
Following are the screenshots of my database.
From the front-end I need to list all categories, and all respective products under each category.
I can't figure out how to bind two different tables and show products related to each category.
you need manyToOne relation with product table and categorie table:
one categorie have one or more product:
you need join between tow table:categorie(id_categorie,name,etc..),product(id_product,name,etc...,#id_categorie)id_categorie:foreignKey in product table.
In SQL table should have foreign keys. In PHP just take all categories and all products. When you list a category just list all of product with have categor_id equal to id of category with you listed:
categories: ID, name
peroducts: ID, name, category_id
List:
category 1
- products: all with have product.category_id = category.id
I am a beginner with database designs. I have two tables shops and products with a many to many relation with a pivot table product_shop.
In the shop table, this how the structure looks like
shops table
id
name
user_id
products table
id
name
product_shop
product_id
shop_id
location
id
name
shop_id
Now my question is, i want to get all products belonging to a particular user or location, can i modify the products table to add (user_id & location_id) so as not to write complex queries.
Does it also foul the rules of database design?
I have a few linked tables in my custom forum: categories, sub_categories and posts
basically, I have people able to add up to three categories and five sub-categories when they make a new post.
I also enable people to 'listen' to certain categories and sub-categories and have them in an easy to access bar at the side of the page.
My tables are set up thus (only showing relavent fields for ease):
posts:
id INT
category_id VARCHAR(12)
sub_category_id VARCHAR(35)
categories:
id INT
name VARCHAR(20)
sub_categories:
id INT
name VARCHAR(20)
in my posts table, I store the set categories and sub-categories by their ID in the following format:
category_id [2][4][8]
sub_category_id [1][2][3][4][5]
thus enabling me to execute the following query in PHP and get the post based on category and sub-category:
SELECT * FROM posts WHERE category_id LIKE '%[{$id}]%' AND sub_category_id LIKE '%[{$id2}]%'
the problem I have is selecting the sub_categories for the access bar that people 'listen' to...
$sql = "SELECT title, id FROM categories";
$query = mysql_query($sql);
$list = array();
while ($row = mysql_fetch_assoc($query)){
$list[$row['title']] = array();
$sql = "SELECT sub_categories.title FROM sub_categories, posts WHERE (category_id LIKE '%[{$row['id']}]%') AND (????) LIMIT 0,100";
$query = mysql_query($sql);
while($result = mysql_fetch_assoc($query)){
$list[$row['title']][] = $result['title'];
}
}
print_r($list);
Obviously you can see where I am stuck (????), but before I explain what I am trying to do, I'll explain what the output I am looking for is.
when I print the $list array, I want it to print a multi-dimensional array featuring the categories as the first key, with their values being an array of sub-categories that have been tagged in the main category.
The problem I have is that in my sub_category_id field on the post table, remember the values are stored in the format [1][2][3] and I need to check the value against the subcategory field id.
I have tried the following:
"SELECT sub_categories.title FROM sub_categories, posts WHERE (category_id LIKE '%[{$row['id']}]%') AND (sub_category_id LIKE '%[sub_categories.id]%') LIMIT 0,100"
But it didn't work. I don't know whether there is an error in my query or whether it even SHOULD work, but I would be grateful if anyone could tell me how to do it or where I am going wrong in my code!
NB. I am trying to find which sub_categories appear in which categories based on people tagging them in a post together.
You're facing problems because you're not aware about few concepts in database...
In your case, you want to create associations called "many-to-many".
It means that a category can be used in many post and a post can be represented by many category.
To translate that into a "SQL" world, you have to create an intermediate table.
this table will store both identifier of the two tables.
for exemple:
-------------------
| categorisation |
-------------------
| post_id | => is the link to posts
| category_id | => is the link to categories
-------------------
When you create a new post, you create a new object in the table post. But you also create N records in the table categorisation.
When you want to retrieve which categories applied to this post, you can do a query like that:
SELECT post.id, post.name
FROM post
INNER JOIN categorisation on (post.id = categorisation.post_id)
INNER JOIN category ON (categorisation.category_id = category.id)
I think you need to read some articles on the web about database before progressing in you project ;)
I have 2 MySQL tables:
categories (id int, name varchar(1000))
And
products (id int, name varchar(1000))
Each product can be under multiple categories. I'm thinking of adding column "category_ids" into the table 'products' with category Ids separated by semicolons, but this method is inconvenient for MySQL query.
Any other possible methods?
Create a table that matches products with categories:
product_id category_id
1 1
1 2
2 5
3 5
3 2
etc. Hope it helps :)
make third table which have refernce to both table as in below image
Add a junction table linking the two:
**product_categories**
productid (FK ref product.id)
categoryid (FK ref categories)
That seems to be a many to many relationship....
In order to map many to many relationship, u will need another table
categories_products(id, category_id, product_id)
so one product can come under many categories and similarly one category can hold many products.
Product table will have one to many relationship with categories_products table
Categories table will also have one to many relationip with categories_products table
thats a standard way to implement many to many relationships
I want to design a database for an e-commerce application with category/subcategory management. Please suggest a database schema where we can create categories and subcategories and add products to those categories. Each product can have multiple categories and we can select products belong to multiple categories using a boolean database query
Thanks
For categories and sub-categories to any level, along with products belonging to multiple categories, I would start with:
Categories:
category_id
parent_category_id foreign key (Categories.category_id)
... other category information ...
primary key (category_id)
Products:
product_id
... other product information ...
primary key (product_id_id)
ProductCategories:
product_id foreign key (Products.product_id)
category_id foreign key (Categories.category_id)
primary key (category_id,product_id)
index (product_id)
This way you can reach your goal of a hierarchy of categories as well as achieving a many-to-many relationship between products and categories.
You can select a product ID belonging to multiple categories (e.g., 3 and 4) with a query like:
select a.product_id
from Products a, Products b
where a.product_id = b.product_id
and a.category_id = 3
and b.category_id = 4
This would be best solved with a join table. That is, you have a products table, and a categories table, each with a primary key. Then, you create a third table, which is the join table. It has 3 columns: it's primary key, category_id, and product_id. Then, when you want to add a relationship between a product and a category, you insert a row into the join table with the category_id and product_id that are related. You can then use the 3 tables together with joins to display the relationships.