Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

该方法从v2.5.0版本开始提供,用于同时查询数据记录列表及总数量,一般用于分页查询场景中,简化分页查询逻辑。

方法定义:该方法用于同时查询数据记录列表及总数量,一般用于分页查询场景中。方法定义如下:

Code Block
languagego
// 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)

...