diff --git a/chain.go b/chain.go index 3b95caad..bacce7b2 100644 --- a/chain.go +++ b/chain.go @@ -159,6 +159,14 @@ func (s *Chain) First(out interface{}, where ...interface{}) *Chain { 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 { s.do(out).query(where...) return s diff --git a/gorm_test.go b/gorm_test.go index 1f525dee..d9b9af99 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -797,3 +797,11 @@ func TestSoftDelete(t *testing.T) { 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") + } +} diff --git a/main.go b/main.go index a27f8332..1308151f 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,14 @@ func (s *DB) First(out interface{}, where ...interface{}) *Chain { 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 { return s.buildChain().Find(out, where...) }