Cake newb here.
I have two tables. Users and Events. An user can subscribe to Multiple events.
What is the best way to implement this?
Do I have to create another table and link them or is there any other better approach.
If I do create a new table, how do i link them in cake model?
As said by jQuery.PHP.Magento.com you should use HABTM relationship but the name of the third table should be events_users because the table names should be in alphabetical order.
From the doc:
Table names are in alphabetical order by convention. It is possible to
define a custom table name in association definition.
You should use HABTM relationship.
Reason
See users will subscribe to Multiple events and
One event have multiple users subscribed for.
So this is two way relationship. Therefore you need following tables
users : To store user's data,
events : To store user's data,
events_users : To store Which user joined Which event and Vice versa(Events with n number of users)
So users_events will have 2 fields user_id , event_id , both are foreign keys and here you dont need primary key in HABTM relationship.
Related
How to change cakephp concept user belongsTo Group, to user hasMany Group,
apart make new table called users_groups, and what should I change to make my user really have access to many groups that user have?
Make Things Simple:
In such scenario you already needed three tables.
Users table
id
name
phone
Groups
id
group(title)
User_Groups
id
user_id
group_id
As you need relations like user has many groups then you need extra table which at least should include foreign key like user_id, and group_id.So don't hesitate to add extra table for making things simple.
You'd be using 3 tables, and then a HasAndBelongsToMany relationship over the Users_Groups. Users HABTM Groups
How i do a crud with a relationship between a user and services order? With the code, i can't add users in a team for service order. When cakebake, the controller TeamHasUsersController is created, but the form add don't have any field.
The crud has generated with cake bake.
In service_orders: change requester_users_id into user_id, the icon in front of the field will change to a red key (foreign key). Cake conventions need a foreignkey to be named [model]_id.
The name of table with associations should be "team_users".
Three database tables are required for a BelongsToMany association. In the example above we would need tables for articles, tags and articles_tags. The articles_tags table contains the data that links tags and articles together. The joining table is named after the two tables involved, separated with an underscore by convention. In its simplest form, this table consists of article_id and tag_id.
habtm relationship
My data structure is as follows:
Company hasMany Regions
Region hasMany Markets
Market hasMany Stores
Store hasMany Employees
I also have the appropriate belongsTo where necessary.
I used foreign keys for associations. For example, each store has market_id.
When I delete a Company record, the correct Region is also deleted. However, it occurred to me that I also need all associated Markets, Stores, and Employees to be deleted. Or if I deleted a Market, I would need all Stores and Employees deleted.
What is the most appropriate manner of accomplishing this?
Would I add additional foreign keys to the tables? For example, would Stores need region_id and company_id in addition to market_id?
Use dependent association:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany
dependent: When dependent is set to true, recursive model deletion is
possible. In this example, Comment records will be deleted when their
associated User record has been deleted.
You don't need to add the additional foreign keys.
I have some user attributes in different areas so I want to separate them into two tables.
One is the users table with user ID. Another table holds location coordinates of the user. It's still one location per user. It's optional value for the user.
Because It seems to me that users table is too important to mess around and it won't change as much as the location table, I want to make location table a new table. Is this good practice?
But then can I associate these 2 tables as one-to-many and belongs-to still? So I can still use them as normal associated models in controllers? Only they are actually in one to one assocation.
It is usually more simple to deal only with one table, but you can still doing it if you want to.
You can use the hasOne relation for it.
You should use it in both models.
You can read more about it here:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasone
I was wondering if this is possible to do with a has many through
contents orgs folders
id id id
name name title
link
join table - folder_elements
id
element_id
order
contents has many folders through folder_elements
orgs has many folders through folder_elements
So my question is, is it possible to use element_id to store the content.id or the org.id ?
Alex
What you're describing is a sort of HABTM (HasAndBelongsToMany) association. You could define the inter-model associations as you suggest, but there's a pretty significant design flaw: how would you account for a situation where the same ID is used for an orgs record and a contents record, and wish to associate them with the same folder record? It would be impossible to differentiate between the two in your joining table.
There are a number of ways to work around this, but they require more program logic or a more disorganised database schema that presents challenges in maintainability.
The more elegant, robust solution is to rationalise your database structure. If there's no pressing need to have One Join Table to Rule Them All, you could design your model associations like this:
Content hasMany ContentsFolder belongsTo Folder
Folder hasMany ContentsFolder belongsTo Content
Org hasMany OrgsFolder belongsTo Folder
Folder hasMany OrgsFolder belongsTo Org
This is the internal structure of a HABTM association defined explicitly so you can define fields in the joining table. Your contents and folders tables would remain the same, but your two joining tables would look like:
contents_folders
-> id
-> content_id
-> order
orgs_folders
-> id
-> org_id
-> order
Unfortunately, yes, this would require defining five models instead of three, but such are the limitations of CakePHP's object-relational modelling.