Skip to content

Commit

Permalink
fix relation all one to many
Browse files Browse the repository at this point in the history
  • Loading branch information
fifsky committed Dec 26, 2018
1 parent 025e958 commit 370d4ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 3 additions & 1 deletion relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ func RelationAll(data interface{} , chains map[string] BuilderChainFunc) error {
// Combine relation data as a one-to-one relation
fmap := make(map[interface{}]reflect.Value)
for n := 0; n < reflect.Indirect(fi).Len(); n++ {
fmap[mapper.FieldByName(refVal.Index(n), relations[0]).Interface()] = reflect.Indirect(fi).Index(n)
val := reflect.Indirect(fi).Index(n)
fid := mapper.FieldByName(val, relations[1])
fmap[fid.Interface()] = val
}

// Set the result to the model
Expand Down
13 changes: 7 additions & 6 deletions testdata/test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ INSERT INTO `moments` (`id`, `user_id`, `content`, `comment_total`, `like_total`
VALUES
(1,5,'sdfsdfsdfsdfsdf',0,0,1,'2018-11-28 14:04:02','2018-11-28 14:04:02'),
(2,5,'sdfsdfsdfsdfsdf',0,0,1,'2018-11-28 17:14:23','2018-11-28 17:14:23'),
(3,5,'123123123',0,0,1,'2018-11-28 17:19:38','2018-11-28 17:19:38'),
(3,6,'123123123',0,0,1,'2018-11-28 17:19:38','2018-11-28 17:19:38'),
(4,5,'13212312313',0,0,1,'2018-11-28 17:22:25','2018-11-28 17:22:25'),
(5,5,'123123123123',0,0,1,'2018-11-28 17:24:21','2018-11-28 17:24:21'),
(6,5,'131231232345tasvdf',0,0,1,'2018-11-28 17:24:27','2018-11-28 17:24:27'),
(6,6,'131231232345tasvdf',0,0,1,'2018-11-28 17:24:27','2018-11-28 17:24:27'),
(7,5,'1231231231231231',0,0,1,'2018-11-28 18:07:48','2018-11-28 18:07:48'),
(8,5,'1231231231231231',0,0,1,'2018-11-28 18:09:20','2018-11-28 18:09:20'),
(9,5,'1231231231231231',0,0,1,'2018-11-28 18:11:19','2018-11-28 18:11:19'),
(9,6,'1231231231231231',0,0,1,'2018-11-28 18:11:19','2018-11-28 18:11:19'),
(10,5,'1231231231231231',0,0,1,'2018-11-28 18:13:52','2018-11-28 18:13:52'),
(11,5,'1231231231231231',0,0,1,'2018-11-28 18:15:02','2018-11-28 18:15:02'),
(12,5,'1231231231231231',0,0,1,'2018-11-28 18:15:13','2018-11-28 18:15:13'),
(13,5,'1231231231231231',0,0,1,'2018-11-28 18:15:39','2018-11-28 18:15:39'),
(14,5,'开开信息想你',0,0,1,'2018-11-28 18:31:37','2018-11-28 18:31:37'),
(14,6,'开开信息想你',0,0,1,'2018-11-28 18:31:37','2018-11-28 18:31:37'),
(15,5,'网友们已经开始争相给宝宝取名字了',0,0,1,'2018-11-28 18:34:45','2018-11-28 18:34:45'),
(16,5,' B2B事业部的对外报价显示',0,0,1,'2018-11-28 18:35:24','2018-11-28 18:35:24');
(16,6,' B2B事业部的对外报价显示',0,0,1,'2018-11-28 18:35:24','2018-11-28 18:35:24');

/*!40000 ALTER TABLE `moments` ENABLE KEYS */;
UNLOCK TABLES;
Expand Down Expand Up @@ -138,7 +138,8 @@ LOCK TABLES `moment_users` WRITE;

INSERT INTO `moment_users` (`id`, `type`, `openid`, `nickname`, `avatar`, `city`, `country`, `gender`, `province`, `created_at`, `updated_at`)
VALUES
(5,1,'ovEry5Nu-V873UoenVcchvnsL9Ng','豆爸&玥爸','https://wx.qlogo.cn/mmopen/vi_32/ajNVdqHZLLAa1A2huibNjHbb5zWCfLDrPzNJAOHcCgflUOthH1ZeU7lVZZEelsckFU9jB2JOFVLfEgyTeWc0AjA/132','Songjiang','China',1,'Shanghai','2018-11-28 10:29:55','2018-11-28 10:29:55');
(5,1,'ovEry5Nu-V873UoenVcchvnsL9Ng','豆爸&玥爸','https://wx.qlogo.cn/mmopen/vi_32/ajNVdqHZLLAa1A2huibNjHbb5zWCfLDrPzNJAOHcCgflUOthH1ZeU7lVZZEelsckFU9jB2JOFVLfEgyTeWc0AjA/132','Songjiang','China',1,'Shanghai','2018-11-28 10:29:55','2018-11-28 10:29:55'),
(6,1,'ovEcy5Nu-V873UoenVcchvnsL9Ng','呵呵','https://wx.qlogo.cn/mmopen/vi_32/ajNVdqHZLLAa1A2huibNjHbb5zWCfLDrPzNJAOHcCgflUOthH1ZeU7lVZZEelsckFU9jB2JOFVLfEgyTeWc0AjA/132','Songjiang','China',1,'Shanghai','2018-11-28 10:29:55','2018-11-28 10:29:55');

/*!40000 ALTER TABLE `moment_users` ENABLE KEYS */;
UNLOCK TABLES;

0 comments on commit 370d4ec

Please sign in to comment.