Document SubStruct

This commit is contained in:
Jinzhu 2013-11-03 10:31:36 +08:00
parent e2927e1ec5
commit bd54a03505
2 changed files with 26 additions and 21 deletions

View File

@ -93,6 +93,24 @@ user := User{Name: "jinzhu", Age: 18, Birthday: time.Now()}
db.Save(&user) db.Save(&user)
``` ```
### Create With SubStruct
```go
user := User{
Name: "jinzhu",
BillingAddress: Address{Address1: "Billing Address - Address 1"},
ShippingAddress: Address{Address1: "Shipping Address - Address 1"},
Email: []Email{{Email: "jinzhu@example.com"}, {Email: "jinzhu-2@example@example.com"}},
}
db.Save(&user)
//// INSERT INTO "addresses" (address1) VALUES ("Billing Address - Address 1");
//// INSERT INTO "addresses" (address1) VALUES ("Shipping Address - Address 1");
//// INSERT INTO "users" (name,billing_address_id,shipping_address_id) VALUES ("jinzhu", 1, 2);
//// INSERT INTO "emails" (user_id,email) VALUES (111, "jinzhu@example.com");
//// INSERT INTO "emails" (user_id,email) VALUES (111, "jinzhu-2@example.com");
```
## Query ## Query
```go ```go

View File

@ -1090,26 +1090,13 @@ func TestSubStruct(t *testing.T) {
db.Save(&comment3) db.Save(&comment3)
} }
func TestT(t *testing.T) { func TestForReadme(t *testing.T) {
user := User{Name: "jinzhu", Age: 18, Birthday: time.Now()} user := User{
Name: "jinzhu",
BillingAddress: Address{Address1: "Billing Address - Address 1"},
ShippingAddress: Address{Address1: "Shipping Address - Address 1"},
Email: []Email{{Email: "jinzhu@example.com"}, {Email: "jinzhu-2@example@example.com"}},
}
db.Save(&user) db.Save(&user)
var user2 User
debug("asdsd")
db.Where("name in (?)", []string{"1"}).First(&user2)
debug("aaadsd")
debug(user2)
db.Model(&user2).Updates(User{Name: "lala", Age: 10})
debug("aals")
//// UPDATE users SET name='hello' WHERE id=111;
var users []User
// db.Where("name = '3'").Or(User{Name: "2"}).Find(&users)
db.Where("name = '3'").Or(map[string]interface{}{"name": "2"}).Find(&users)
var count int64
db.Model(User{}).Where("name = ?", "jinzhu").Count(&count)
db.Table("users").Count(&count)
debug(count)
} }