Friday, November 5, 2010

Has One vs belongsTo and a weird table association

Hello people.

I'm developing a Cake app and have a little problem. Let me tell you
how is it organized first. The site i'm building is about a "city
shop guide", i mean, a site where you can search for shops. The issue
is that there can be two types of users, which can be either "regular
users" or "shop owners".

Regular users are people who enter the site and make searches. They
can save shops in their personal "favorites" page and make some other
things like leave comments.

Shop owners, are, people who owns a shop and upload information about
it in the site.

So, i've thinking in two ways of creating this and use all the
functionality of Auth and ACL.

The first one is to have a table "users", like regular, and in each
"specific user", a reference to it something like this:

CREATE TABLE users(
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(30),
password VARCHAR(50),
role VARCHAR(10),
PRIMARY KEY(id)
);

CREATE TABLE shop(
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
address VARCHAR(50),
category SMALLINT,
user_id INTEGER UNSIGNED NOT NULL,
PRIMARY KEY(id)
)

class shop extends AppModel{

var $belongsTo = array("user");

// OR

var $hasOne = array("user");

}

Then, all users which have a "shop" associated can upload and edit
information about the shop they owns. I think this would work well,
but i don't know which type of association to include (belongsTo or
hasOne).

Another approach would be

CREATE TABLE users(
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(30),
password VARCHAR(50),
role VARCHAR(10),
shop_id INTEGER UNSIGNED NULL, -- Nullable
regular_user_id INTEGER UNSIGNED NULL, -- Nullable
PRIMARY KEY(id)
);

CREATE TABLE shop(
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
address VARCHAR(50),
category SMALLINT,
user_id INTEGER UNSIGNED NOT NULL,
PRIMARY KEY(id)
)

CREATE TABLE regular_user(
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
last_name VARCHAR(50),
email VARCHAR(50),
user_id INTEGER UNSIGNED NOT NULL,
PRIMARY KEY(id)
)

Then, if a user can "know" which type of "user" is just reading it's
not null field. This is a type of inheritence in database, that's
broadly used.

So, what do you think guys? Do you have some expirience with this?

Thank you very much!

--
Santiago Basulto.-

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: