mirror of https://github.com/go-gorm/gorm.git
Document Attrs in README
This commit is contained in:
parent
0a47d75870
commit
721090d39a
23
README.md
23
README.md
|
@ -93,6 +93,16 @@ db.Where(User{Name: "Jinzhu"}).FirstOrInit(&user)
|
||||||
db.FirstOrInit(&user, map[string]interface{}{"name": "jinzhu", "age": 20})
|
db.FirstOrInit(&user, map[string]interface{}{"name": "jinzhu", "age": 20})
|
||||||
//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}
|
//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}
|
||||||
|
|
||||||
|
// FirstOrInit With Attrs
|
||||||
|
db.Where(User{Name: "noexisting_user"}).Attrs(User{Age: 20}).FirstOrInit(&user)
|
||||||
|
//// user -> select * from users where name = 'noexisting_user';
|
||||||
|
//// user -> User{Name: "noexisting_user", Age: 20}
|
||||||
|
db.Where(User{Name: "Jinzhu"}).Attrs(User{Age: 20}).FirstOrInit(&user)
|
||||||
|
//// user -> select * from users where name = 'jinzhu';
|
||||||
|
//// user -> User{Id: 111, Name: "Jinzhu", Age: 18}
|
||||||
|
//// Do you notice the difference? The value in Attrs won't be used when search,
|
||||||
|
//// But is used to initalize the object if no record found
|
||||||
|
|
||||||
// FirstOrCreate
|
// FirstOrCreate
|
||||||
db.FirstOrCreate(&user, User{Name: "noexisting_user"})
|
db.FirstOrCreate(&user, User{Name: "noexisting_user"})
|
||||||
//// user -> User{Id: 112, Name: "noexisting_user"}
|
//// user -> User{Id: 112, Name: "noexisting_user"}
|
||||||
|
@ -101,6 +111,15 @@ db.Where(User{Name: "Jinzhu"}).FirstOrCreate(&user)
|
||||||
db.FirstOrCreate(&user, map[string]interface{}{"name": "jinzhu", "age": 20})
|
db.FirstOrCreate(&user, map[string]interface{}{"name": "jinzhu", "age": 20})
|
||||||
//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}
|
//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}
|
||||||
|
|
||||||
|
// FirstOrCreate With Attrs
|
||||||
|
db.Where(User{Name: "noexisting_user"}).Attrs(User{Age: 20}).FirstOrCreate(&user)
|
||||||
|
//// user -> select * from users where name = 'noexisting_user';
|
||||||
|
//// user -> User{Id: 112, Name: "noexisting_user", Age: 20}
|
||||||
|
db.Where(User{Name: "Jinzhu"}).Attrs(User{Age: 20}).FirstOrCreate(&user)
|
||||||
|
//// user -> select * from users where name = 'jinzhu';
|
||||||
|
//// user -> User{Id: 111, Name: "Jinzhu", Age: 18}
|
||||||
|
//// You must noticed that the Attrs is similar to FirstOrInit with Attrs, yes?
|
||||||
|
|
||||||
// Select
|
// Select
|
||||||
db.Select("name").Find(&users)
|
db.Select("name").Find(&users)
|
||||||
//// users -> select name from users;
|
//// users -> select name from users;
|
||||||
|
@ -264,6 +283,10 @@ db.Where("mail_type = ?", "TEXT").Find(&users1).Table("deleted_users").First(&us
|
||||||
//// users1 -> select * from users where mail_type = 'TEXT';
|
//// users1 -> select * from users where mail_type = 'TEXT';
|
||||||
//// users2 -> select * from deleted_users where mail_type = 'TEXT';
|
//// users2 -> select * from deleted_users where mail_type = 'TEXT';
|
||||||
|
|
||||||
|
db.Where("email = ?", "x@example.org"').Attrs(User{FromIp: "111.111.111.111"}).FirstOrCreate(&user)
|
||||||
|
//// user -> select * from users where email = 'x@example.org'
|
||||||
|
//// (if no record found) -> INSERT INTO "users" (email,from_ip) VALUES ("x@example.org", "111.111.111.111")
|
||||||
|
|
||||||
// Open your mind, add more cool examples
|
// Open your mind, add more cool examples
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue