New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with Find() and First() #291
Comments
type Customer struct { func (c Customer) TableName() string { func MyMiddleware(rw http.ResponseWriter, r *http.Request) {
} |
This is the problem: FIRST COMMAND: Currently it does not work when &u is not a reference to a slice. &u should point to &Customer{} and not &[]Customer{}. This function should also NEVER accept an extra parameter for the primary key. That is what the FIND function is for. FIND COMMAND: If you want all values, then use "ALL" COMMAND. db.ALL(&u) where &u=[]Customer{} http://daylerees.com/codebright/eloquent-queries ALL COMMAND: db.All(&u), where &u=[]Customer{} GET COMMAND: |
Hello @pjebs Although I stole the ideas from ActiveRecord, it doesn't works exactly like it, but works like below: db.First(&User{}) // first user, sort by primary key
db.First(&[]User{}) // find the first user, and put it into the array
db.Find(&User{}) // first user
db.Find(&[]User{}) // find all users
// find records with conditions
db.First(&User{}, 123)
db.First(&User{}, "name = ?", "hello")
db.Find(&User{}, "name = ?", "hello")
db.Where("name = ?", "hello").First(&User{})
db.Where("name = ?", "hello").Find(&User{}) |
I'm not sure if this problem is with your code or documentation.
I also assume you designed GORM based on Ruby On Rails or PHP-Laravel which is why I believe there is a mistake.
http://laravel.com/docs/4.2/eloquent
I don't expect you to agree with all of my suggestions because your trying to make Find() and First() accomplish what 4 commands in Laravel: All(), First(), Find(), Get().
But the downside is that your method ALWAYS returns slice even if it is more convenient to get One Struct (without slice) when you WANT and EXPECT one result.
The text was updated successfully, but these errors were encountered: