Initialize method FirstOrInit, FirstOrCreate

This commit is contained in:
Jinzhu 2013-10-29 18:02:28 +08:00
parent 8a81711e41
commit 2e84f3b216
3 changed files with 24 additions and 0 deletions

View File

@ -159,6 +159,14 @@ func (s *Chain) First(out interface{}, where ...interface{}) *Chain {
return s return s
} }
func (s *Chain) FirstOrInit(out interface{}, where ...interface{}) *Chain {
return s
}
func (s *Chain) FirstOrCreate(out interface{}, where ...interface{}) *Chain {
return s
}
func (s *Chain) Find(out interface{}, where ...interface{}) *Chain { func (s *Chain) Find(out interface{}, where ...interface{}) *Chain {
s.do(out).query(where...) s.do(out).query(where...)
return s return s

View File

@ -797,3 +797,11 @@ func TestSoftDelete(t *testing.T) {
t.Errorf("Can't find out permanently deleted order") t.Errorf("Can't find out permanently deleted order")
} }
} }
func TestFindOrInitialize(t *testing.T) {
var user User
db.Where(User{Name: "hello world"}).FirstOrInit(&user)
if user.Name != "hello world" || user.Id != 0 {
t.Errorf("user should be initialized with search value")
}
}

View File

@ -29,6 +29,14 @@ func (s *DB) First(out interface{}, where ...interface{}) *Chain {
return s.buildChain().First(out, where...) return s.buildChain().First(out, where...)
} }
func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *Chain {
return s.buildChain().FirstOrInit(out, where...)
}
func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *Chain {
return s.buildChain().FirstOrCreate(out, where...)
}
func (s *DB) Find(out interface{}, where ...interface{}) *Chain { func (s *DB) Find(out interface{}, where ...interface{}) *Chain {
return s.buildChain().Find(out, where...) return s.buildChain().Find(out, where...)
} }