Get data from model - Sharykhin/go-modular GitHub Wiki

There are two methods to get data from model: FindById FindAll

FindById

To retrieve data from model first of all you have to create instance of model:

import model "go-modular/application/model/todo"
 ...

todoModel := model.New()

FindById method expects one parameter (Id - value of primary key) and return error.When model finds data, it will fill values into Schema of model

if err := todoModel.FindById(1); err != nil {
   return err
}
fmt.Println(todoModel.GetTitle())
fmt.Println(todoModel.GetId())
fmt.Println(todoModel.GetIsDone())

In this case our instance got values to appropriate columns

FindAll

Let's look at FindAll method. This method returns slice of map ([]map[string]interface{}) and error

if todos,err := todoModel.FindAll(); err != nil {
   return err
}
for _,todo := range todos {
  fmt.Println(todo["title"])
  fmt.Println(todo["id"])
}
```go