From 0ea51c1e1f80dc7202963d50ff42eb818d58854a Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 11 Feb 2015 19:27:58 +0800 Subject: [PATCH] Update README for Preload --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index ce1dae9b..001f25cc 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ The fantastic ORM library for Golang, aims to be developer friendly. * Callbacks (before/after create/save/update/delete/find) * Soft Deletes * Auto Migrations +* Preloading (eager loading) * Transactions * Customizable Logger * Iteration Support via [Rows](#row--rows) @@ -335,6 +336,28 @@ db.Where("name <> ?","jinzhu").Where("age >= ? and role <> ?",20,"admin").Find(& db.Where("role = ?", "admin").Or("role = ?", "super_admin").Not("name = ?", "jinzhu").Find(&users) ``` +### 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 +``` + ## Update ```go