Skip to content
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

Closed
pjebs opened this issue Nov 22, 2014 · 3 comments
Closed

Issue with Find() and First() #291

pjebs opened this issue Nov 22, 2014 · 3 comments

Comments

@pjebs
Copy link

pjebs commented Nov 22, 2014

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.

@pjebs
Copy link
Author

pjebs commented Nov 22, 2014

type Customer struct {
Id uint32 gorm:"column:id; primary_key:yes"
Access_token string gorm:"column:access_token"
}

func (c Customer) TableName() string {
return "customers"
}

func MyMiddleware(rw http.ResponseWriter, r *http.Request) {
// u := []Customer{}
u := &Customer{}

db, _ := gorm.Open("mysql", "root:@/main")
// db.First(&u, 5)
// db.First(&u)
// db.Find(&u, 5)
db.Find(&u)

}

@pjebs
Copy link
Author

pjebs commented Nov 22, 2014

This is the problem:

FIRST COMMAND:
This should return the first result back from the collection of all results being returned. (whether there are extra WHERE parameters or not).
It should parse the ONE result into &Customer{} and NOT have an array with one result in there.

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:
When Find command is given a primary key, it should not return a slice. It should also ALWAYS expect a primary key value.

If you want all values, then use "ALL" COMMAND. db.ALL(&u) where &u=[]Customer{}

http://daylerees.com/codebright/eloquent-queries
http://laravel.com/docs/4.2/queries

ALL COMMAND:
This should be included in your library. That would return all values.

db.All(&u), where &u=[]Customer{}

GET COMMAND:
This should be included in your library. This would return an ARRAY of all values including after you put WHERE constraints on.

@jinzhu
Copy link
Member

jinzhu commented Nov 22, 2014

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{})

@jinzhu jinzhu closed this as completed Nov 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants