我刚刚开始在我的应用程序中使用 FuelPHP,并被堆叠在一个 orm 关系设置中。我有以下 3 个表,并希望使用项目模型显示带有部门用户名的项目列表。但我不知道如何设置与 orm 关系的关系。
你如何使用 $_belongs_to、$_has_one、$_has_many 或 $_many_many 来建立这种关系?
我想做的就像 sql "select *, department.name, users.name from projects left join projects on projects.department_id = department.id left join users on projects.user_id = users.id"
### TABLE projects ###
CREATE TABLE IF NOT EXISTS `projects` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`department_id` int(11) unsigned NOT NULL,
`user_id` int(11) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `department_id` (`department_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `projects`
ADD CONSTRAINT `m_project_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`),
ADD CONSTRAINT `m_project_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
### TABLE departments ###
CREATE TABLE IF NOT EXISTS `departments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
### TABLE users ###
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`login_id` varchar(100) NOT NULL,
`nickname` varchar(20) NOT NULL,
`password` varchar(255) NOT NULL,
`last_login_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
提前致谢。