Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp workaround for model inheritance relationship

Tags:

cakephp

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?

like image 607
user1542639 Avatar asked Nov 03 '22 07:11

user1542639


1 Answers

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.

like image 174
Happy Avatar answered Nov 12 '22 21:11

Happy