mirror of https://github.com/go-gorm/gorm.git
Initialize method FirstOrInit, FirstOrCreate
This commit is contained in:
parent
8a81711e41
commit
2e84f3b216
8
chain.go
8
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
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
8
main.go
8
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...)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue