CakePHP find with 2 HABTM relationships - cakephp

I'm working on project that run on CakePHP 2 framework. In this application I have:
models:
Wallnote, User, Group
relationships:
Group HABTM User
Wallnote HABTM User
Wallnote HABTM Group
tables:
wallnotes
- id
- user_id (owner id)
- ...
users_wallnotes
- user_id
- wallnote_id
groups_wallnotes
- group_id
- wallnote_id
groups_users
- group_id
- user_id
I'm using the relationships "Wallnote HABTM User" and "Wallnote HABTM Group" as a filter i.e. user_id/group_id(6) wallnote_id(10) mean, that wallnote with id 10 will be visible for user with id 6, respectively for all users in group with id 6.
I would like to find all wallnotes matching these conditions:
logged user is an owner of this wallnote OR
wallnote was shared with logged user -> record in table users_wallnotes OR
wallnote was shared with some group and logged user is member of this group
It is possible to do this using find() function?
Thanks for answer.

You have two roles: group and user.
it's very difficult to handle it with User and Group separately...
I think the standard way is you have unique role for each user and group, for handling this you must have this tables:
wallnotes
- id
- user_id (owner id)
- ...
roles
- id
- user_id
- group_id
(each rows in this table have group_id or user_id , no both of them)
roles_wallnotes
- id
- role_id
- wallnote_id
when you create a user(or group) you must create a role for it. So all groups and users, now have unique id.
for example you have 2 groups(with 1,2 ids) and 4 users(with 1,2,3,4 ids), then:
users :
|id|name |
---|------
1 | user1
2 | user2
3 | user3
4 | user4
groups :
|id|name |
---|------
1 | group1
2 | group2
roles :
|id| user_id | group_id |
---|---------|----------|
1 | 1 | null |
2 | 2 | null |
3 | 3 | null |
4 | 4 | null |
5 | null | 1 |
6 | null | 2 |
now you must have this relations:
Role hasOne Group
Role hasOne User
Wallnote HABTM Role
with this solution you can easily use find function for retrieve your data you need...

Related

Drupal users by role - sql query

I need a sql query count list of how many users have more than one role (by role and by role combination) eg:
admin, author: 50
admin, author, annoymouse user, authenticated user: 20
members, authenticated user: 100
etc
Thank you
MySQL query is:
mysql> SELECT ur.rid, r.name, count(*) as TOT from users_roles ur INNER JOIN role r ON r.rid = ur.rid group by ur.rid ;
+-----+---------------+-----+
| rid | name | TOT |
+-----+---------------+-----+
| 3 | administrator | 2 |
| 4 | tst | 1 |
+-----+---------------+-----+
2 rows in set (0,00 sec)
As alternative option, you have the Views Module. Create a new view, type: user. Enable and play with aggregation on the Role field

Adding multiple entries to table

I've got a website where users can make recipes.
So I've got a table called ingredients with ingredients
So the user selects the ingredients he/she wants with the amount required, and then I will store this in a new table called recipes
But I wonder what's the best practice for storing this
I was thinking like this
RECIPES
Id | UserId | IngredientId
--------------------------
0 | 23 | 3,6,7,9,2,1
1 | 56 | 8,2,6,7,1
2 | 80 | 1,7,3,2,7
3 | 11 | 6,7,4,3,21,2
You may consider having join tables. It would work something like this:
User
Id | Name
0 | Bob Smith
1 | Susie Jones
Recipe
Id | Name
0 | Ramen
1 | Pizza
Ingredient
Id | Name
0 | Flour
1 | Water
2 | Yeast
User_Recipe
User_Id | Recipe_Id
0 | 1
1 | 0
Recipe_Ingredient
Recipe_Id | Ingredient_Id | Quantity
1 | 0 | 1 C.
1 | 1 | 1/2 C.
1 | 2 | 1 T.
It depends on how complex your application is. Your table is not normalized as you have written it. On the other hand, my way might be an overkill for a small application, but I would propose the following schema:
Table users
This table contains your user information and each row has a unique user id (as I understand you already have this)
Table ingredients
Here you store your ingredients that also have a unique id. Also, this does not contain any recipe information like quantity but instead has only ingredient attributes like: name, description, form, price_per_kilo, etc.
Table recipes
This table is a many-to-many relationship between users and recipes. Again, it contains only recipe attributes like: user_id, name, date_created, rating, difficulty, instructions, etc
Table recipe_ingredients
Finally, this table lists all the ingredients a recipe requires, including the amount. Columns would be recipe_id, ingredient_id, amount, unit, comment, etc
Hope it helps

SQL Server database design users, groups, roles, memebers

I have the following SQL Server db with one table so far.
----------------
|Users |
----------------
| UserId PK |
| |
| Other fields |
----------------
I need to add few more tables to it which is not a problem but this is the goal:
---------------- ---------------- ---------------- ----------------
|Users | |Roles | | Teams | | Groups |
---------------- ---------------- ---------------- ----------------
| UserId PK | | RoleId PK | | TeamId PK | | GroupId PK |
| | | | | | | |
| Other fields | | Other fields | | Other fields | | Other fields |
---------------- ---------------- ---------------- ----------------
What I need to achieve is the following:
I have X amount of users
User1
User2
UserX
I have 3 roles only for all users to use in all teams and groups
Admin
Member
Visitor
One user can create X amount of teams
Team1
Team2
TeamX
One user can create X amount of groups
Group1
Group2
GroupX
Groups and Teams can have users assigned to them with different roles (Admin, Member, Visitor)
One user can belong to one or many team or groups
One user can belong to one or many roles
I have some hard time understanding the relation between those tables.
Here is what I managed to achieve based on the answer from #Robertas Valeika.
You need 3 more tables.
UsersRoles
UsersRolesGroups
UsersRolesTeams.
Relationships:
UsersRoles - UsersRolesGroups,
Groups - UsersRolesGroups
UsersRoles - UsersRolesTeams,
Teams - UsersRolesTeams
Users - UsersRoles,
Roles - UsersRoles.
And add FK to users in groups and teams tables to identify creator of group and team.

Relationships Between Tables in MS Access

I'm new in DataBases at all and have some difficulties with setting relationships between 3 tables in MS Access 2013.
The idea is that I have a table with accounts info, a table with calls related to this accounts and also one table with all the possible call responses. I tried different combinations between them but nothing works.
1st table - Accounts : AccountID(PK) | AccountName | Language | Country | Email
2nd table - Calls : CallID(PK) | Account | Response | Comment | Date
3rd table - Responses: ResponseID(PK) | Response
When you have a table, it usually has a Primary Key field that is the main index of the table. In order for you to connect it with other tables, you usually do that by setting Foreign Key on the other table.
Let's say you have your Accounts table, and it has AccountID field as Primary Key. This field is unique (meaning no duplicate value for this field).
Now, you have the other table called Calls and you have a Foreign Key field called AccountID there, which points to the Accounts table.
Essentially you have Accounts with the following data:
AccountID| AccountName | Language | Country | Email
1 | FirstName | EN | US | some#email.com
2 | SecondName | EN | US | some#email.com
Now you have the other table Calls with Many calls
CallID(PK) | AccountID(FK) | ResponseID(FK) | Comment | Date
1 | 1 | 1 | a comment | 26/10
2 | 1 | 1 | a comment | 26/10
3 | 2 | 3 | a comment | 26/10
4 | 2 | 3 | a comment | 26/10
You can see the One to Many relationship: One accountID (in my example AccountID=1) to Many Calls (in my example 2 rows with AccountID=1 as foreign keys, rows 1 & 2) and AccountID=2 has also 2 rows of Calls (rows 3 and 4)
Same goes for the Responses table
Using this table structure:
Accounts : AccountID(PK) | AccountName | Language | Country | Email
Calls : CallID(PK) | AccountID(FK) | ResponseID(FK) | Comment | Date
Responses: ResponseID(PK) | Response
Accounts.AccountID is referenced by Calls.AccountID. 1:n – many calls for one account possible, but each call concerns just one account.
Responses.ResponseID is referenced by Calls.ResponseID. 1:n – many calls can get the same response from the prepared set, but each call gets exactly one of them.
To actually define the Relationships in Access, open the Relationships window...
... then follow the detailed instructions here:
How to define relationships between tables in an Access database

databases design

for example i have table users, which have 3 fields:
id - login - password
---------------------
1 | john | *****
2 | jack | *****
3 | jane | *****
now i want that each user could have his own settings.
So, do i need to create three different tables, like
user_N_settings:
id | key | value
-------------------------
1 | save_data | True
or i should create one big table for all users instead?
users_settings:
id | key | value | user_id
---------------------------------------------
1 | save_data | True | 2
2 | some_opt | False | 3
One table for all users. A table per user would be very wrong.
One table. If all the setting values are of the same type then it may make sense to create one row per setting. If the attributes are all very different then create one column per setting.

Resources