From bd54a03505e254222b5905654029d56f17d24cfb Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 3 Nov 2013 10:31:36 +0800 Subject: [PATCH] Document SubStruct --- README.md | 18 ++++++++++++++++++ gorm_test.go | 29 ++++++++--------------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ce14a8d2..06047670 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,24 @@ user := User{Name: "jinzhu", Age: 18, Birthday: time.Now()} 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 ```go diff --git a/gorm_test.go b/gorm_test.go index c6a46164..930602f8 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -1090,26 +1090,13 @@ func TestSubStruct(t *testing.T) { db.Save(&comment3) } -func TestT(t *testing.T) { - user := User{Name: "jinzhu", Age: 18, Birthday: time.Now()} +func TestForReadme(t *testing.T) { + 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) - - 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) - }