model get join tables - php

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

Related

How to insert one table to another from another database with relationships in MySQL?

In my old database, I have 2 tables:
invoices
id
dr_no
customer_name
created_at
invoice_items
id
dr_no
item_name
created_at
That is a one to many relationship. An invoice can have many invoice_item. The problem here is that instead of the id of the invoice as the reference, the dr_no is used.
I'm doing a revamp of the system and want to transfer the old database to a new one and use the invoice_id as the reference instead of the `dr_no.
The current invoices table already holds 100k records and instead of looping it an inserting it in the a new database, I used this code:
INSERT INTO new_db.invoices SELECT * FROM old_db.invoices;
This does the job but to also insert the invoice_items, I have to loop all invoices, check all invoice_items with specific dr_no and insert into new invoice_items table.
Is there a more performant way of doing this?
You could insert into the new invoice_items table using the following query:
INSERT INTO mew_db.invoice_items
SELECT
t.id,
v.id, -- invoice_id
t.item_name,
t.created_at
FROM old_db.invoice_items t
INNER JOIN old_db.invoices v ON v.dr_no = t.dr_no

Database Relationship Schema

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?

Arrange data in MySQL tables

I have a users table and a categories table which already contain data,
I also have a third table which called user_category which has three columns(id,user_id,category_id).
When the user first register he must choose a category or more than one from a dropdown. I want to insert the id of the user to user_id columns and the id of each category to the category_id so if the user choose more than one category he will have more than one record in the user_category table.
Use This
SELECT user_id FROM users
ORDER BY user_id DESC
LIMIT 1
This will give you the last user_id which has inserted Now by using this user id you can insert the category_id by for each loop or for loop

how to insert unique data into other table by looking at the primary key in php

I have two tables which are:
retailoutlets:
idretailoulets (PK),
cities_idcities,
Mall_idMalls,
outletName,
BrandId(fk).
Brand:
BrandId(PK),
BrandName,
companyURL.
Each brand can have multiple outlet in the retail outlet table. i already insert the brand name by sort the unique data from the outlet name then i insert to the brandname.
Now, i wanted to insert the BrandID to the retail outlets table by referring to the brand table
Can someone tell me how to deal with these? thank you
[in this table i wanted to put the brand id, referring the outlet name, because each brand have several outlet name and in the brand table, the id of each brand already assigned
As i can understand from your question, you filled brand table with unique outletName from retailoutlets
i already insert the brand name by sort the unique data from the outlet name then i insert to the brandname
So i can imagine you did something like this:
INSERT INTO `brand` (BrandName)
SELECT outletName as BrandName FROM retailoutlets GROUP BY outletName
If so, then you just need to
UPDATE retailoutlets O
INNER JOIN brand B ON B.BrandName = O.outletName
SET O.BrandId = B.BrandId

Propel 2 double join on custom field

I'm trying to create a query with propel that joins a first table called Entry with a second table called Company that have 3 fields related to a third table called User.
Entry
id
field1
field2
company_id
Company
id
field1
field2
user_id1
user_id2
user_id3
User
id
name
surname
team
I want to be able to join Entry to Company by company_id and then join to the table User using the field userid1.
I tried different ways like
EntryQuery::create()->joinWith('Company')->joinWith("User")
but I get and error saying that Entry has no relation with user
or
EntryQuery::create()->useCompanyQuery()->joinWith("User")->endUse()
but I still get an error that Company has no relation with User, even though all the user fields in Company have a relation with User in the database.
There is any way to specify the field on the join?
I finally found a way to do it, I'm not sure if it's the best one but works.
EntryQuery::create()
->useCompanyQuery()
->innerJoinUserRelatedByUserId1()
->endUse()
->with("Company")
->with("UserRelatedByUserId1")
This hydrates all the relations.

Categories