php - Manage multiple tables relationed in CakePHP -
so, have 3 tables
table: users
_________________ | id | user | ------------------- | 1 | roy | | 2 | ben | |________|________|
table: hability_lists // set list habilities available
___________________________________ | id | category | subcategory | ------------------------------------ | 1 | programmer | php | | 2 | programmer | asp | |________|____________|_____________|
table: habilities // set habilities users
________________________________________ | id | user_id | hability_list_id | ----------------------------------------- | 1 | 2 | 1 | | 2 | 1 | 2 | |________|____________|__________________|
by this, can see that:
roy asp programmer , ben php programmer
but, how set relative models using cakephp? know how using 2 models not using 3 models.
there way this? or maybe better way do?
thanks in advance.
when working mvc framework it's highly recommended follow its conventions. few changes may beneficial you.
what looking habtm (has , belongs many) association between "users" table , "habilities" table *. i'm guessing, design of table, user can have multiple habilities, otherwise should check hasmany association.
it should this:
table habilities: select * habilities;
+----+------------+----------------------+----------+ | id | category | subcategoy | created | modified | +----+------------+------------+---------+----------+ | 1 | programmer | asp | | | | 2 | programmer | php | | | | 3 | musician | classical | | | +----+------------+------------+---------+----------+
table users: select * users;
+----+-------+---------------------+---------------------+ | id | name | created | modified | ** +----+-------+---------------------+---------------------+ | 1 | roy | 2012-08-15 02:52:18 | 2013-01-17 03:25:28 | | 2 | ben | 2012-11-10 03:36:12 | 2012-11-10 03:36:12 | +----+-------+---------------------+---------------------+
relational table habtm: select * habilities_users;
+----+-------------+-------------------+----------+ | id | hability_id | user_id | created | modified | +----+-------------+---------+---------+----------+ | 1 | 1 | 2 | | | | 2 | 2 | 1 | | | +----+-------------+---------+---------+----------+
look reference columns in habilities_users, they're singular _id suffix work cakephp.
defining models classes it's important, since it's define associations.
app/model/user.php
<?php class user extends appmodel { public $hasandbelongstomany = array('hability'); }
app/model/hability.php
<?php class hability extends appmodel { public $hasandbelongstomany = array('user'); }
the table habilities_users doesn't need model file, behaviours , properties implicit in declaration of associated models.
* using names it's cakephp way. [link]
** adding "created" , "modified" in each table store events each record automatically.
Comments
Post a Comment