I have 3 tables set up in my database so far:
Users / User_Profiles / Addresses
The addresses table is used for storing address both from users and eventually from companies and such in the future. The User_Profiles table has more information about a user that doesn't involve addresses.
Users Table:
id | username | password | etc....
User_Profiles:
id | user_id | phone | website | bio | address_id | shipping_address_id
Addresses
id | address | city | state | zip | etc...
Here's my issue. I want to find a user by id, then return all of his information - his info from user_profiles and his info from addresses (both address_id and shipping_address_id point to the addresses table).
In my model I currently have this:
public function getUserInfo($user_id){
return $this->find()
->contain(['user_profiles'=>['fields'=>['user_id','phone_number', 'website', 'bio', ]]])
->where(['id' => $user_id])
->first();
}
This is simple enough and returns my user_profile information. Now I need to join the addresses table (twice, I guess?) for getting the shipping address and the regular address (by shipping_address_id and address_id).
How can I do this? I figured I could use TableRegistry to load in the user_profiles table to store the address_id and shipping_address_id as variables, and then work my way from there, but it seems overly complicated.
Ideally I'd want all of the information returned in an object - the user_profile info along with any addresses associated.