我想制作一个类似电子邮件的邮件系统,以便在Intranet上使用。数据库设计如下所示。 我想知道如何建立最好的模型关联。
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 ;
答案 0 :(得分:4)
因此,您在users_messages
表中添加了其他信息,因此它不适用于普通的HABTM。您需要创建另一个模型UsersMessage.php
您的模型关联可能看起来像(如果我不错过任何一点) -
public $hasMany = array('User');
public $belongsTo = array('User');
public $hasMany = array('UsersMessage');
public $belongsTo = array('User');
public $hasMany = array('UsersMessage');
public $belongsTo = array('Group');
public $hasMany = array('UsersMessage');
public $useTable = 'users_messages';
public $belongsTo = array('User', 'Message', 'ImFolder');
有关详细信息,请参阅 - hasMany through (The Join Model)