diff --git a/advanced.html b/advanced.html index 18644d79..447c2377 100644 --- a/advanced.html +++ b/advanced.html @@ -70,7 +70,7 @@ data-chapter-title="Advanced Usage" data-filepath="advanced.md" data-basepath="." - data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)" + data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)" data-innerlanguage=""> @@ -355,7 +355,7 @@
Refer Associations for more details
+By default, GORM will save associations also when creating, you could skip it by set gorm:save_associations
to false
db.Set("gorm:save_associations", false).Create(&user)
+
You could define default value in the gorm
tag, then the inserting SQL will ignore these fields that has default value and its value is blank, and after insert the record into databae, gorm will load those fields's value from database.
type Animal struct {
@@ -1097,6 +1109,29 @@ db.Table("deleted_users").Find(&d
db.Table("deleted_users").Where("name = ?", "jinzhu").Delete()
//// DELETE FROM deleted_users WHERE name = 'jinzhu';
+db.Preload("Orders").Find(&users)
+//// SELECT * FROM users;
+//// SELECT * FROM orders WHERE user_id IN (1,2,3,4);
+
+db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
+//// SELECT * FROM users;
+//// SELECT * FROM orders WHERE user_id IN (1,2,3,4) AND state NOT IN ('cancelled');
+
+db.Where("state = ?", "active").Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
+//// SELECT * FROM users WHERE state = 'active';
+//// SELECT * FROM orders WHERE user_id IN (1,2) AND state NOT IN ('cancelled');
+
+db.Preload("Orders").Preload("Profile").Preload("Role").Find(&users)
+//// SELECT * FROM users;
+//// SELECT * FROM orders WHERE user_id IN (1,2,3,4); // has many
+//// SELECT * FROM profiles WHERE user_id IN (1,2,3,4); // has one
+//// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to
+
+db.Preload("Orders.OrderItems").Find(&users)
+db.Preload("Orders", "state = ?", "paid").Preload("Orders.OrderItems").Find(&users)
+
Save
will include all fields when perform the Updating SQL, even it is not changed
By default, GORM will save associations also when updating, you could skip it by set gorm:save_associations
to false
db.Set("gorm:save_associations", false).Save(&user)
+
// Add extra SQL option for updating SQL
db.Model(&user).Set("gorm:update_option", "OPTION (OPTIMIZE FOR UNKNOWN)").Update("name, "hello")
diff --git a/database.html b/database.html
index 0d2f6cd4..bbd62c97 100644
--- a/database.html
+++ b/database.html
@@ -70,7 +70,7 @@
data-chapter-title="Database"
data-filepath="database.md"
data-basepath="."
- data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)"
+ data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage="">
@@ -355,7 +355,7 @@
-
+
3.3.
diff --git a/development.html b/development.html
index 7afaa845..b22e514c 100644
--- a/development.html
+++ b/development.html
@@ -70,7 +70,7 @@
data-chapter-title="Development"
data-filepath="development.md"
data-basepath="."
- data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)"
+ data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage="">
@@ -355,7 +355,7 @@
-
+
3.3.
diff --git a/documents/SUMMARY.md b/documents/SUMMARY.md
index 7fb70aa7..67e784c9 100644
--- a/documents/SUMMARY.md
+++ b/documents/SUMMARY.md
@@ -17,7 +17,7 @@
* [CRUD: Reading and Writing Data](curd.md)
* [Create](curd.md#create),
* [Query](curd.md#query),
- * [Preloading (Eager Loading)](curd.md#preloading),
+ * [Preloading (Eager Loading)](curd.md#preloading-eager-loading),
* [Update](curd.md#update),
* [Delete / Soft Delete](curd.md#delete)
* [Callbacks](callbacks.md)
diff --git a/documents/curd.md b/documents/curd.md
index 7f677a0f..39b4957e 100644
--- a/documents/curd.md
+++ b/documents/curd.md
@@ -42,6 +42,14 @@ db.Create(&user)
Refer [Associations](associations.html) for more details
+#### Skip Save Associations when creating
+
+By default, GORM will save associations also when creating, you could skip it by set `gorm:save_associations` to `false`
+
+```go
+db.Set("gorm:save_associations", false).Create(&user)
+```
+
### Default Values
You could define default value in the `gorm` tag, then the inserting SQL will ignore these fields that has default value and its value is blank, and after insert the record into databae, gorm will load those fields's value from database.
@@ -524,6 +532,35 @@ db.Table("deleted_users").Where("name = ?", "jinzhu").Delete()
//// DELETE FROM deleted_users WHERE name = 'jinzhu';
```
+## Preloading (Eager loading)
+
+```go
+db.Preload("Orders").Find(&users)
+//// SELECT * FROM users;
+//// SELECT * FROM orders WHERE user_id IN (1,2,3,4);
+
+db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
+//// SELECT * FROM users;
+//// SELECT * FROM orders WHERE user_id IN (1,2,3,4) AND state NOT IN ('cancelled');
+
+db.Where("state = ?", "active").Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
+//// SELECT * FROM users WHERE state = 'active';
+//// SELECT * FROM orders WHERE user_id IN (1,2) AND state NOT IN ('cancelled');
+
+db.Preload("Orders").Preload("Profile").Preload("Role").Find(&users)
+//// SELECT * FROM users;
+//// SELECT * FROM orders WHERE user_id IN (1,2,3,4); // has many
+//// SELECT * FROM profiles WHERE user_id IN (1,2,3,4); // has one
+//// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to
+```
+
+### Nested Preloading
+
+```go
+db.Preload("Orders.OrderItems").Find(&users)
+db.Preload("Orders", "state = ?", "paid").Preload("Orders.OrderItems").Find(&users)
+```
+
## Update
### Update All Fields
@@ -636,6 +673,14 @@ func (user *User) BeforeSave(scope *gorm.Scope) (err error) {
}
```
+### Skip Save Associations when updating
+
+By default, GORM will save associations also when updating, you could skip it by set `gorm:save_associations` to `false`
+
+```go
+db.Set("gorm:save_associations", false).Save(&user)
+```
+
### Extra Updating option
```go
diff --git a/documents/preload.md b/documents/preload.md
new file mode 100644
index 00000000..53a455fe
--- /dev/null
+++ b/documents/preload.md
@@ -0,0 +1,4 @@
+# CRUD: Reading and Writing Data
+
+Preloading (Eager loading)
+
diff --git a/gitbook/docs/faq.md b/gitbook/docs/faq.md
index 57fdf3be..0b57b6c6 100644
--- a/gitbook/docs/faq.md
+++ b/gitbook/docs/faq.md
@@ -18,6 +18,10 @@ You should always use paths and the `.md` extensions when linking to your files,
Yes, GitBooks can be created in [sub-directories](structure.md#subdirectory). GitBook.com and the CLI also looks by default in a serie of [folders](structure.md).
+#### Does GitBook supports RTL languages?
+
+Yes, GitBook automatically detect the direction in your pages (`rtl` or `ltr`) and adjust the layout accordingly. The direction can also be specified globally in the [book.json](config.md).
+
---
#### Does GitBook support Math equations?
diff --git a/gitbook/docs/plugins/api.md b/gitbook/docs/plugins/api.md
new file mode 100644
index 00000000..c5a17b3d
--- /dev/null
+++ b/gitbook/docs/plugins/api.md
@@ -0,0 +1,89 @@
+# Context and APIs
+
+GitBooks provides different APIs and contexts to plugins. These APIs can vary according to the GitBook version being used, your plugin should specify the `engines.gitbook` field in `package.json` accordingly.
+
+#### Book instance
+
+The `Book` class is the central point of GitBook, it centralize all access read methods. This class is defined in [book.js](https://github.com/GitbookIO/gitbook/blob/master/lib/book.js).
+
+```js
+// Read configuration from book.json
+var value = book.config.get('title', 'Default Value');
+
+// Resolve a filename to an absolute path
+var filepath = book.resolve('README.md');
+```
+
+#### Output instance
+
+The `Output` class represent the output/write process.
+
+```js
+// Return root folder for the output
+var root = output.root();
+
+// Resolve a file in the output folder
+var filepath = output.resolve('myimage.png');
+
+// Convert a filename to an URL (returns a path to an html file)
+var fileurl = output.toURL('mychapter/README.md');
+
+// Write a file in the output folder
+output.write('hello.txt', 'Hello World')
+ .then(function() { ... });
+
+// Copy a file to the output folder
+output.copyFile('./myfile.jpg', 'cover.jpg')
+ .then(function() { ... });
+
+// Verify that a file exists
+output.hasFile('hello.txt')
+ .then(function(exists) { ... });
+```
+
+#### Page instance
+
+A page instance represent the current parsed page.
+
+```js
+// Title of the page (from SUMMARY)
+page.title
+
+// Content of the page (Markdown/Asciidoc/HTML according to the stage)
+page.content
+
+// Relative path in the book
+page.path
+
+// Absolute path to the file
+page.rawPath
+
+// Type of parser used for this file
+page.type ('markdown' or 'asciidoc')
+```
+
+#### Context for Blocks and Filters
+
+Blocks and filters have access to the same context, this context is bind to the template engine execution:
+
+```js
+{
+ // Current templating syntax
+ "ctx": {
+ // For example, after a {% set message = "hello" %}
+ "message": "hello"
+ },
+
+ // Book instance
+ "book" ,
+
+ // Output instance
+ "output":