How to setup model association in CakePHP? -
i want make messaging system email use on intranet. database design shown below. want know how make best model associations.
create table if not exists `groups` ( `id` int(10) unsigned not null auto_increment, `name` varchar(60) not null, `created` datetime not null, `modified` datetime not null, primary key (`id`) ) engine=innodb default charset=utf8 auto_increment=1 ; create table if not exists `im_folders` ( `id` int(10) unsigned not null auto_increment, `name` varchar(255) not null, `user_id` int(11) not null, `created` datetime not null, `modified` datetime not null, primary key (`id`) ) engine=innodb default charset=utf8 auto_increment=1 ; create table if not exists `messages` ( `id` int(10) unsigned not null auto_increment, `subject` varchar(255) not null, `body` text, `user_id` int(11) not null, `created` datetime default null, `modified` datetime default null, primary key (`id`) ) engine=innodb default charset=utf8 auto_increment=1 ; create table if not exists `users` ( `id` int(10) unsigned not null auto_increment, `group_id` int(11) not null, `username` varchar(60) not null, `email` varchar(255) not null, `password` varchar(60) not null, `name` varchar(255) not null, `firstname` varchar(255) not null, `lastname` varchar(255) not null, `created` datetime not null, `modified` datetime not null, primary key (`id`) ) engine=innodb default charset=utf8 auto_increment=1 ; create table if not exists `users_messages` ( `id` int(10) unsigned not null auto_increment, `user_id` int(11) not null, `message_id` int(11) not null, `im_folder_id` int(11) not null, `issender` tinyint(1) not null, `isread` tinyint(1) not null, `isstarred` tinyint(1) not null, `isdeleted` tinyint(1) not null, `created` datetime not null, `modified` datetime not null, primary key (`id`) ) engine=innodb default charset=utf8 auto_increment=1 ;
thus added other information on users_messages table, not work normal habtm. need create model usersmessage.php
your model association might looks as(if not miss points)-
group.php
public $hasmany = array('user'); imfolder.php
public $belongsto = array('user'); public $hasmany = array('usersmessage'); message.php
public $belongsto = array('user'); public $hasmany = array('usersmessage'); user.php
public $belongsto = array('group'); public $hasmany = array('usersmessage'); usersmessage.php
public $usetable = 'users_messages'; public $belongsto = array('user', 'message', 'imfolder'); for more details read-hasmany through (the join model)
Comments
Post a Comment