Вопрос

I want to make a messaging system like email for use on an intranet. The database design is shown below. I want to know how to make the 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 you added other information on users_messages table, so it will not work with normal HABTM. You need to create another Model as UsersMessage.php

Your model association might looks like as(if i not miss any 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)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top