[英]Relation Between Two Tables: make relationship between two tables
I have two tables as post and gallery , and i have made a relationship gallery to post table. 我有两个表作为post和gallery ,并且我已经建立了一个关系画廊来发布表。 My requirement is, When user upload content it store in the post table(content field) , If user upload the images are video i want to store the images/video name in, gallery table and the gallery id refers to the post table.
我的要求是,当用户上载内容时,将其存储在帖子表中(内容字段),如果用户上载图像是视频,则我想将图像/视频名称存储在图库表中,而图库ID是指帖子表。 I dont know how to do it.
我不知道该怎么做。 please any one help me?
请有人帮我吗?
post table: 发布表:
CREATE TABLE IF NOT EXISTS `post` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`gallery_id` bigint(20) unsigned NOT NULL,
`content` longtext,
`photo` varchar(128) DEFAULT NULL,
`video` varchar(128) DEFAULT NULL,
`created` timestamp NULL DEFAULT NULL,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_forum_post_user` (`user_id`),
KEY `fk_forum_post_gallery` (`gallery_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
ALTER TABLE `post`
ADD CONSTRAINT `fk_post_gallery` FOREIGN KEY (`gallery_id`) REFERENCES `gallery` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
gallery table 画廊表
CREATE TABLE IF NOT EXISTS `gallery` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`type` int(11) NOT NULL DEFAULT '1' COMMENT '1- Photo, 2-Video, 3-Documents, 4-Unknown',
`profile_picture` varchar(50) DEFAULT NULL,
`forum_image` varchar(200) DEFAULT NULL,
`forum_video` varchar(200) DEFAULT NULL,
`forum_video_link` varchar(200) DEFAULT NULL,
`created` timestamp NULL DEFAULT NULL,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_gallery_user` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=60 ;
is any other idea is to do or how can i move forward? 还有其他想法吗,或者我该如何前进?
There are a lot of ways you can perform this tasks. 您可以通过多种方式执行此任务。 you can perform logic on code level as well as on database level.
您可以在代码级别以及数据库级别执行逻辑。 however here is a quick answer .
但是,这是一个快速答案。 hope this could help you
希望这可以帮助您
gallery_id
) as it is possible that user may have more than one images or video for single post or user may not want to upload any image / video in this case your gallery id would be null. gallery_id
),因为用户可能有多个图像或视频用于单个帖子,或者用户可能不想上传任何图像/视频,在这种情况下,您的画廊ID将为null。 I did not test the query, so it's just a basic idea 我没有测试查询,所以这只是一个基本思路
Select p.id, p.content, p.created, g.type, g.profilepicture, g.forum_image
from post p, gallery g, postgalleryrelation pgr
where p.id = pgr.postid
and g.id = pgr.galleryid
and p.id = 1
It's just an idea. 这只是一个主意。 You can do much better.
您可以做得更好。
YII have relation
option while creating CRUD
using gii
. 当使用
gii
创建CRUD
YII具有relation
选项。 For that, you have to create tables with foreign key relationship. 为此,您必须创建具有外键关系的表。 So YII will automatically create the relation in coding level.
因此,YII将自动在编码级别创建关系。 You have to choose the option
Build Relation
while creating model for both tables. 为两个表创建模型时,必须选择选项
Build Relation
。
Check the Yii relation tutorials for more information 查看Yii关系教程以获取更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.