We know that the database configuration supports configuring a default database, so the DB
object and Model
object are bound to a specific database when initialized. There are several ways to switch databases at runtime (assuming our databases include a user
user database and an order
database):
-
Achieved through different configuration groups. This requires configuring different group settings in the configuration file, and then you can obtain a singleton object of a specific database in the program via
g.DB("group name")
. -
Switch the database of a singleton object at runtime using the
DB.SetSchema
method. Note that since the database configuration of the singleton object is modified, the impact is global:g.DB().SetSchema("user-schema")
g.DB().SetSchema("order-schema") -
Create a
Schema
database object using the chaining operationSchema
method, and create a model object through this database object to perform subsequent chaining operations:g.DB().Schema("user-schema").Model("user").All()
g.DB().Schema("order-schema").Model("order").All() -
You can also set the corresponding database for the current chaining operation using the chaining operation
Model.Schema
method. If not set, the default connected database of theDB
orTX
is used:g.Model("user").Schema("user-schema").All()
g.Model("order").Schema("order-schema").All()tipNote the difference between the two usage methods: the former creates a
Model
object after theSchema
object and then executes operations; the latter achieves database switching by modifying the database name of the currentModel
object operation. -
Additionally, if the current database operation's configured user has permission, cross-domain operations can be achieved directly by including the database name in the table name, even for cross-domain association queries:
// SELECT * FROM `order`.`order` o LEFT JOIN `user`.`user` u ON (o.uid=u.id) WHERE u.id=1 LIMIT 1
g.Model("order.order o").LeftJoin("user.user u", "o.uid=u.id").Where("u.id", 1).One()