Introduction
This method has been provided since version v2.5.0 and is used for querying both the data record list and the total count, generally used in paging query scenarios to simplify paging query logic.
Method definition:
// AllAndCount retrieves all records and the total count of records from the model.
// If useFieldForCount is true, it will use the fields specified in the model for counting;
// otherwise, it will use a constant value of 1 for counting.
// It returns the result as a slice of records, the total count of records, and an error if any.
// The where parameter is an optional list of conditions to use when retrieving records.
//
// Example:
//
// var model Model
// var result Result
// var count int
// where := []interface{}{"name = ?", "John"}
// result, count, err := model.AllAndCount(true)
// if err != nil {
// // Handle error.
// }
// fmt.Println(result, count)
func (m *Model) AllAndCount(useFieldForCount bool) (result Result, totalCount int, err error)
When querying the total count inside the method, the Limit/Page operations in the query will be ignored.
Usage Example
Basic Query
// SELECT `uid`,`name` FROM `user` WHERE `status`='deleted' LIMIT 0,10
// SELECT COUNT(`uid`,`name`) FROM `user` WHERE `status`='deleted'
all, count, err := Model("user").Fields("uid", "name").Where("status", "deleted").Limit(0, 10).AllAndCount(true)
// SELECT `uid`,`name` FROM `user` WHERE `status`='deleted' LIMIT 0,10
// SELECT COUNT(1) FROM `user` WHERE `status`='deleted'
all, count, err := Model("user").Fields("uid", "name").Where("status", "deleted").Limit(0, 10).AllAndCount(false)
With PageCache
Starting from version v2.9.8, AllAndCount supports configuring different caching strategies for count queries and data queries through the PageCache method:
import (
"time"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// Set different cache durations for count and data queries
result, total, err := g.Model("user").Ctx(ctx).PageCache(
gdb.CacheOption{
Duration: time.Hour, // count query cached for 1 hour
Name: "user-count",
Force: false,
},
gdb.CacheOption{
Duration: 5 * time.Minute, // data query cached for 5 minutes
Name: "user-data",
Force: false,
},
).Where("status", "active").Limit(0, 10).AllAndCount(false)
For more cache configuration information, please refer to: ORM Model - Query Cache