Question

I've just read the Redbean Documentation, and it's awesome! But I have some questions before I proceed.

I want to create a User data structure composed in a particular way. On the site I'm working on, I have three types of users:

  1. The professionist
  2. The company (with a contact person to represent the company)
  3. The student

I think the best way to implement this user data structure is to have a person table (that has all the common data), and other three tables for the professionist/company/student unique data.

A user on my site will be then a composition of Person <---> Professionist for example, but this is not a one-to-many relationship (i think).

How can I achieve this in MySQL? Or better in Redbean?

Was it helpful?

Solution

A lot of developers tend to make a butt load of tables to do what they want, and although this does work, it can get extremely complicated (especially if other developers need to do updates on your code).

What I would do is create a table called "users" and then have a row called 'type' which will allow you to assign that specific user either professionist, company or student.

so it would look something like this:

$user = R::dispense('users');
$user->name = 'Name Goes Here';
$user->type = 'Student';
R::store($user);

And you can add whatever other data you need and should be dynamic but this gives you a simplified version of what you would need.

The only other thing you can do is create a relation table that specifies the different account types, and then when you add in your user info in the "users" table just replace $user->type with $user->type_id and relate the typeid with the inserted user, but to me that just seems like more work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top