This commit is contained in:
John Guo
2023-05-30 11:59:00 +08:00
committed by GitHub
parent 4914517f6b
commit aa8eabd853
3 changed files with 56 additions and 3 deletions

View File

@ -726,3 +726,37 @@ func Test_Issue2561(t *testing.T) {
t.Assert(one[`create_time`], ``)
})
}
// https://github.com/gogf/gf/issues/2439
func Test_Issue2439(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
array := gstr.SplitAndTrim(gtest.DataContent(`issue2439.sql`), ";")
for _, v := range array {
if _, err := db.Exec(ctx, v); err != nil {
gtest.Error(err)
}
}
defer dropTable("a")
defer dropTable("b")
defer dropTable("c")
orm := db.Model("a")
orm = orm.InnerJoin(
"c", "a.id=c.id",
)
orm = orm.InnerJoinOnField("b", "id")
whereFormat := fmt.Sprintf(
"(`%s`.`%s` LIKE ?) ",
"b", "name",
)
orm = orm.WhereOrf(
whereFormat,
"%a%",
)
r, err := orm.All()
t.AssertNil(err)
t.Assert(len(r), 1)
t.Assert(r[0]["id"], 2)
t.Assert(r[0]["name"], "a")
})
}

View File

@ -0,0 +1,19 @@
CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id) USING BTREE
) ENGINE = InnoDB;
INSERT INTO `a` (`id`) VALUES ('2');
CREATE TABLE `b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL ,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB;
INSERT INTO `b` (`id`, `name`) VALUES ('2', 'a');
INSERT INTO `b` (`id`, `name`) VALUES ('3', 'b');
CREATE TABLE `c` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB;
INSERT INTO `c` (`id`) VALUES ('2');

View File

@ -67,7 +67,7 @@ func (m *Model) InnerJoin(table ...string) *Model {
func (m *Model) LeftJoinOnField(table, field string) *Model {
return m.doJoin("LEFT", table, fmt.Sprintf(
`%s.%s=%s.%s`,
m.tables,
m.tablesInit,
m.db.GetCore().QuoteWord(field),
m.db.GetCore().QuoteWord(table),
m.db.GetCore().QuoteWord(field),
@ -82,7 +82,7 @@ func (m *Model) LeftJoinOnField(table, field string) *Model {
func (m *Model) RightJoinOnField(table, field string) *Model {
return m.doJoin("RIGHT", table, fmt.Sprintf(
`%s.%s=%s.%s`,
m.tables,
m.tablesInit,
m.db.GetCore().QuoteWord(field),
m.db.GetCore().QuoteWord(table),
m.db.GetCore().QuoteWord(field),
@ -97,7 +97,7 @@ func (m *Model) RightJoinOnField(table, field string) *Model {
func (m *Model) InnerJoinOnField(table, field string) *Model {
return m.doJoin("INNER", table, fmt.Sprintf(
`%s.%s=%s.%s`,
m.tables,
m.tablesInit,
m.db.GetCore().QuoteWord(field),
m.db.GetCore().QuoteWord(table),
m.db.GetCore().QuoteWord(field),