Database Relationship Schema - php

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?

Related

How to update table using many to many relations in Mysql

I wanted to update a table using many to many relations. I have three tables:
users;
badges;
user_badge;
In the users table I have: id, username and game_count. In the badges table I have: badge_id and badge_name. The user_badge table has id, user_id and badge_id.
The user_id and badge_id in the user_badge table represent the id columns in the users and badges tables. These are used as foreign keys in the user_badge table.
I would like to update the user_badge table with the values from the other two tables: users and badges.
For example, table Users, have the following details.
id:1 username:john game_count:1
Table badges has:
id:1 badge_name:firstbadge
So, when I apply a condition (e.g. game_count=1), the user_badge
table should update its column with user_id from users and badge_id from the badges table.
Can someone help me with writing this query?

PHP how to make a relationship between two tables

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

Query issue in Yii

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.

How to create product table in which each product is under multiple categories in MySQL?

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

model get join tables

I am new to cakephp. I have table customers with this
id, product_id, created, modified
and my product table has this
id, label, cost
I want to select all the records from customer table which has the product_id in product table (join on id may be)?
How can I do that
You should write a hasMany association in your model for the table you would like to query, then do your $this->Model->find('all'...) query and make sure in your options you specify recursive=>1 so that it will get your associated data. http://book.cakephp.org/view/1043/hasMany

Categories