name: Models

sort: 4

Creating models

Models are normally the best way to handle the numerous databases used in web applications. The bee new project does not contain an example of models. Demos on implementing and using models can instead be found in bee api projects.

The Controller can automatically handle models for simple applications.

Larger applications with more reusable code requiring logic separation must use models. Reusable logic can be factored out into a Model and used to handle database interactions. The following is an example:

  1. package models
  2. import (
  3. "loggo/utils"
  4. "path/filepath"
  5. "strconv"
  6. "strings"
  7. )
  8. var (
  9. NotPV []string = []string{"css", "js", "class", "gif", "jpg", "jpeg", "png", "bmp", "ico", "rss", "xml", "swf"}
  10. )
  11. const big = 0xFFFFFF
  12. func LogPV(urls string) bool {
  13. ext := filepath.Ext(urls)
  14. if ext == "" {
  15. return true
  16. }
  17. for _, v := range NotPV {
  18. if v == strings.ToLower(ext) {
  19. return false
  20. }
  21. }
  22. return true
  23. }

Please see MVC Models for the specific examples of database models and Beego’s ORM framework. The next section will cover writing views.