I have three tables (blocks, roles, users) where the following is true in the schema:
blocks have many users
users have many blocks
users have many roles, but the roles are specific to the block (and the user)
blocks have many roles, but the roles are specific to the user (and the block)
For example:
user => john
belongs to block => 'metro' with role => 'builder'
belongs to block => 'rural' with role => 'investor'
user => dan
belongs to block => 'metro' with role => 'resident'
belongs to block => 'rural' with role => 'investor'
I have a join table named block_role_user with the following columns:
id block_id role_id user_id created modified
I guess this would be a hasandbelongstomany relationship amongst all three tables. I thought about laying this out with a hasandbelongstomany table between blocks and users and another hasandbelongstomany table between roles and users but this will not fully define the unique relationship between a block, role and user.
I'm looking for advice on the best way to approach this and appreciate any advice.
I've looked into binding the model, but this doesn't seem to apply for the unique three part relationship I have here.
I thought about creating a model called blockroleuser and setting up the relationships using standard cakephp convention. If this is an acceptable way, can you give some guidance on naming the model and controller files?
Thanks again...
If I got you right, then you are looking for a relation less loose than HABTM, which will define a specific connection between a User, a Block and a Role.
Personally I would probably use a model that defines these relations using belongs to, where a User can have many of those, something like:
User hasMany Whatever
Whatever belongsTo User
belongsTo Block
belongsTo Role
This is probably more or less what you ment when you were talking about a BlockRoleUser model, the proper naming might depend on what exactly blocks are. Of course a generic name like for example Involvement or even UserBlockRole might be fine too, I don't think you should bother too much about this.
Anyways, this looks pretty straightforward and strict to me, and if necessary for your application, then additional HABTM relations could be set up too utilizing the same table. These HABTM relations would most probably only be used for selects only, for example when there's need for querying all the Blocks a User is related to, or when testing whether a User is related to a specific Block, etc...
Related
I'm creating a db schema that involves users that can have multiple users.
I want to register different companies to use the web services.
For example:
user A or B (etc) can signup and create a company account
user A can create multiple accounts of other users with their types, similarly user B
If user A or B create different accounts, how would I know this particular user is belong to User A or B company ? I think user table have many to many relationship with itself (like basic friendship design).
Please suggest the best design .
Ex.
User 3,4 belongs to User A
User 5,6 belongs to User B
In general, I would recommend starting by identifying all the entities you are trying to persist. It sounds like you have two distinct entities in your question. One being "user," which represents a single person. Your second entity is "company." A "user" can belong to a company.
An example of a database design would be one table for users, and one table for companies. In the "users" table, you would want to have a foreign key column that references the primary key (unique id) of the company the user belongs to. If each user can only belong to one company, this becomes a simple one to many relationship.
In short, I would highly recommend treating company accounts separately from user accounts, since they are fundamentally different entities.
Is it better to create tables that store a lot of data that are related to an entity (User for example) or many tables to store said data?
For example:
User Table
Name
Email
Subscription Id
Email Notifications
Permissions
Or
User Table
Name
Email
Subscription Table
User ID
Subscription ID
Notification Table
User ID
Receives?
... etc
Please consider code in this as well, or I would have posted to ServerVault.
From a relational design standpoint what is important is the normal form you're aiming for. In general, if the "column" would require multiple values (subscription_id1, subscription_id2, etc) then it is a repeating group, and that would indicate to you that it needs to be moved to a related table. You've provided very general table and column notes, but taking a cue from the fact that you named "Email Notifications" and "Permissions" with plurals, I'm going to assume that those require related tables.
From my understanding, cakephp doesn't support database inheritance relationship. However, I want to create a database with different type of Users.
In this case, there are three types of Users: Seller, Customer, and Administrator. Every users should have basic User information such as password, username, etc.
However, each types of users will have its own unique set of datas. For example, seller may have inventory_id while customer may have something like delivery_address, etc.
I have been thinking of creating a workaround to this problem without destroying cakephp convention. I was going to create three additional foreign keys, admin_id, seller_id and customer_id, inside User table, which links to other table. However, knowing that this is an IS-A relationship not HAS-A, I would have to make sure that two of the ids are NULL value. Therefore, this workaround seems ugly to me..
Is there any other simpler, better approach?
For this type of database structure I would probably look at adopting an Entity-Attribute-Value model. This would mean your customer may have a delivery_address and your user may have an inventory_id but as far as your relationship in Cake is concerned your both your user and customer would just have an attribute_id ... you can then create another table that stores what type of attributes are available.
It it's simplest form, your user and customer would be attached to an *attribute_lookup* or *attribute_link* table by a hasMany (probably) relationship. That attribute_lookup/link table would be connected by a belongsTo/hasOne relationship to the actual Attribute Type and Attribute Value models.
Providing that you normalise your tables correctly, you can stick well within Cake relationship conventions.
You can read more about EAV here.
I have been thinking about this problem for some time now, and I have eventually got around to build a solution for it. What I came up with is a new ORM that can be used on top of CakePHP.
It sort of works as CakePHP 3.0 with entities that represent an id/model, but it is much more advanced and supports multi table inheritance and single table inheritance.
Check it out: https://github.com/erobwen/Cream
By ref table I'm referring to a kind of entity 'type' table,
and by storage table I'm referring to a table that stores a lot of changing information.
For example:
I have a 'user' table named as such, which is a storage table since it can hold an indeterminate amount of users.
Then i have 'roles' table, which holds role information, it is a type table, as there are many users for each role.
I then have a 'profiles' table, which hold a one to one relationship with the 'user' table.
Now, I've tried this:
user
userrole
userprofile
However, this convention to me implies that the roles and profile tables each have a one to one relationship with user whereas I know the role table does not.
How to people usually name tables for semantic purposes for the example I described?
The Oracle convention works well here.
plural for normal tables (Users for a table of Users. The table itself is a store of many users, so I name it as I would describe the data within it)
User_Roles would be a distinct list of roles.
User_Role_Assignments a list of roles for users. I would imagine you would want a many to many here if a user can have any role and a role can be assigned to any user.
User_Profiles would be a distinct list of profiles.
User_Profile_Assignments for a table which was a many to many relationship between users and profiles.
If you have a one to one relationship, then one of the tables should have a key to the other. Users should have a profile_id if there is a one to one relationship.
I would like to make a model agency based on codeigniter, but im a but stuck with the database, exactly the registration part.
I would like to allow users to sign up as, model, photohgrapher, agency, or make-up artist.
So could someoone give me an opinion how to make the database? Like seperate the models, photographers, agencies, and artists in diferent tables, and at the registration form only ask for baseic info? like name, password, email, D.O.B., or there is a nother way?.
Thank you
You should use entity sub-typing with a parent type of "USER", which will contain your basic information, and with sub-types of "MODEL", "AGENCY", "PHOTOGRAPHER", "MAKEUP_ARTIST". This will allow you to have a better user experience for the inevitable case where there is overlap. I'm sure there are photographers who have agencies and agencies that do make-up etc. It would be much better for these types of users to have a single user ID and password despite having different types of profiles.
Make a drop down for different type of people signing up which the data for drop down comes from a separate table (e.g. person_type) from database and save the basic details of the person in separate table with the ID of the person_type table.
You can make a model for getting, inserting and updating records for this purpose.