[Question]数据表格和另一个表进行关联后显示异常的问题
代码版本:1.2.16
问题描述 [详细地描述问题,让大家都能理解]
概述:在一个数据表格中,其中一个字段是从另一张表关联后获取展示的,在列表页能正常显示,但进入编辑页和详情页后,该字段没有任何显示输出 举例:一张文章表(articles),是数据表格的主表,另一张是文章内容表(articles_content),两张表关联后,显示articles_content表中的 content 字段,在列表页能正常显示,但点击该文章的编辑和详情页时,content字段的内容是空白
示例代码 [如果有必要,展示代码,线上示例,或仓库]
info := articles.GetInfo() .... info.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", }) .... formList := articles.GetForm() .... formList.AddField("Content","content", db.Text, form.Text) .... detail := articles.GetDetail() .... detail.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", }) ...
其他信息 [如截图等其他信息可以贴在这里]
是以上使用方法不对吗
贴下两张表的结构
è´´ä¸ä¸¤å¼ 表çç»æ
CREATE TABLE articles (
id int(11) NOT NULL AUTO_INCREMENT COMMENT 'èµè®¯ID',
author_id int(11) NOT NULL DEFAULT '0' COMMENT 'ä½è
ID',
author_name varchar(50) NOT NULL DEFAULT '' COMMENT 'ä½è
',
short_title varchar(100) DEFAULT '' COMMENT 'çæ é¢',
title varchar(200) NOT NULL DEFAULT '' COMMENT 'æ é¢',
source varchar(200) NOT NULL DEFAULT '' COMMENT 'æ¥æº',
pic_url text COMMENT 'å¾çå°å',
pic_type tinyint(4) DEFAULT '0' COMMENT '6ç§ç±»å:,0:æ å¾,1:ä¸å¼ å°å¾,2:ä¸å¼ 大å¾,3:ä¸å¼ å°å¾,4:å°å¾è§é¢,5:大å¾è§é¢',
status tinyint(4) DEFAULT '0' COMMENT 'ç¶æ:0:è稿,1.å¾
å®¡æ ¸ 2.å®¡æ ¸éè¿,3.æªéè¿,4.æ¤å,5.å é¤',
is_top tinyint(4) DEFAULT '0' COMMENT 'æ¯å¦ç½®é¡¶:0:æªç½®é¡¶,1.置顶',
sort_index int(4) DEFAULT '0' COMMENT 'æåºåå',
create_time int(11) DEFAULT '0' COMMENT 'å建æ¶é´',
update_time int(11) DEFAULT '0' COMMENT 'ä¿®æ¹æ¶é´',
top_time int(11) DEFAULT '0' COMMENT '置顶æ¶é´',
deleted int(1) DEFAULT '0' COMMENT 'å é¤ï¼0ï¼æªå é¤ï¼1ï¼å é¤',
deleted_time bigint(11) DEFAULT '0' COMMENT 'å 餿¶é´',
PRIMARY KEY (id) USING BTREE,
KEY idx_status (status,author_id) USING BTREE,
KEY idx_is_top (status,is_top) USING BTREE,
KEY idx_create_time (status,create_time) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8mb4 COMMENT='èµè®¯è¡¨';
CREATE TABLE articles_content (
article_id int(11) NOT NULL DEFAULT '0' COMMENT 'èµè®¯ID',
content text COMMENT 'å
容',
PRIMARY KEY (article_id) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='èµè®¯å
容表';
// viewer: A 表
// customer_name: B 表中要展示的栏位名称
viewer.AddField(lang.Username(), "customer_name", db.Bigint).
FieldJoin(types.Join{
Table: "ez_customer", // B 表名称
JoinField: "id", // B 表栏位
Field: "uid", // A 表栏位
})
Hope this can help you
我的问题里面就是这样做的,但是显示不出来,,打印sql语句是这样的:
{"statement": "select articles.author_id,articles.author_name,articles.short_title,articles.title,articles.source,articles.pic_url,articles.pic_type,articles.status,articles.is_top,articles.sort_index,articles.id,group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content from articles left join articles_content on articles_content.article_id = articles.id GROUP BY articles.id order by articles.id desc LIMIT ? OFFSET ?", "args": [10,0]}
发现其中有这句话:
group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content
// viewer: A 表 // customer_name: B 表中要展示的栏位名称 viewer.AddField(lang.Username(), "customer_name", db.Bigint). FieldJoin(types.Join{ Table: "ez_customer", // B 表名称 JoinField: "id", // B 表栏位 Field: "uid", // A 表栏位 })Hope this can help you
我的问题里面就是这样做的,但是显示不出来: info.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", })
打印sql语句是这样的:
{"statement": "select articles.author_id,articles.author_name,articles.short_title,articles.title,articles.source,articles.pic_url,articles.pic_type,articles.status,articles.is_top,articles.sort_index,articles.id,group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content from articles left join articles_content on articles_content.article_id = articles.id GROUP BY articles.id order by articles.id desc LIMIT ? OFFSET ?", "args": [10,0]}
发现其中有这句话:
group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content
这个 和 实际的 content 字段是不匹配的
我的问题里面就是这样做的,但是显示不出来,,打印sql语句是这样的: {"statement": "select articles.
author_id,articles.author_name,articles.short_title,articles.title,articles.source,articles.pic_url,articles.pic_type,articles.status,articles.is_top,articles.sort_index,articles.id,group_concat(articles_content.contentseparator 'Z5dDrivernSKR') as articles_content_goadmin_join_content fromarticlesleft joinarticles_contenton articles_content.article_id= articles.idGROUP BY articles.idorder by articles.iddesc LIMIT ? OFFSET ?", "args": [10,0]}发现其中有这句话: group_concat(articles_content.
contentseparator 'Z5dDrivernSKR') as articles_content_goadmin_join_content// viewer: A 表 // customer_name: B 表中要展示的栏位名称 viewer.AddField(lang.Username(), "customer_name", db.Bigint). FieldJoin(types.Join{ Table: "ez_customer", // B 表名称 JoinField: "id", // B 表栏位 Field: "uid", // A 表栏位 })Hope this can help you
我的问题里面就是这样做的,但是显示不出来: info.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", })
打印sql语句是这样的: {"statement": "select articles.
author_id,articles.author_name,articles.short_title,articles.title,articles.source,articles.pic_url,articles.pic_type,articles.status,articles.is_top,articles.sort_index,articles.id,group_concat(articles_content.contentseparator 'Z5dDrivernSKR') as articles_content_goadmin_join_content fromarticlesleft joinarticles_contenton articles_content.article_id= articles.idGROUP BY articles.idorder by articles.iddesc LIMIT ? OFFSET ?", "args": [10,0]}发现其中有这句话: group_concat(articles_content.
contentseparator 'Z5dDrivernSKR') as articles_content_goadmin_join_content 这个 和 实际的 content 字段是不匹配的
我在 info.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", }) 后面加了 .FieldSubstr(0,30) 就不能正常显示,去掉这句话,在列表中能正常显示
进入详情页后,也无法正常显示出 content,即使加了这样的代码 detail := articles.GetDetail() detail.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", }) 也不行
detail.AddField("Content", "content", db.Text ).
后面的 db.Text 是错误的,这里填写的是 detail 表 id 的 data type
如果没理解错,下面的代码应该可以工作
detail.AddField(DISPLAY_NAME, "content", db.Bigint).
FieldJoin(types.Join{
Table: "articles_content",
JoinField: "article_id",
Field: "id",
})
Add following
FieldDisplay(func(value types.FieldModel) interface{} {
return fmt.Sprintf("%v", value.Row["articles_content_goadmin_join_content"])
})
Thank @Egor86 you saved me time! Xiexie