更新从appengine .GetAll获取的实体并保存到数据存储中。

huangapple go评论112阅读模式
英文:

Update entity from appengine .GetAll and save to datastore

问题

我有一个模型:

  1. type UserProfile struct {
  2. UserID string `datastore:"user_id" json:"user_id,omitempty"`
  3. Username string `datastore:"username" json:"username,omitempty"`
  4. StripUsername string `datastore:"strip_username" json:"strip_username,omitempty"`
  5. Email string `datastore:"email" json:"email,omitempty"`
  6. LoginType string `datastore:"login_type" json:"login_type,omitempty"`
  7. CurrentSession string `datastore:"current_session" json:"current_session,omitempty"`
  8. FBAcessToken string `datastore:"fb_access_token" json:"fb_access_token,omitempty"`
  9. Created time.Time `datastore:"created" json:"-"`
  10. }

然后我执行一个 .GetAll 来填充它:

  1. // 这里省略了一些步骤
  2. var userProfiles []UserProfile
  3. q.GetAll(c, &userProfiles)

假设我想修改其中一个实体:

  1. userProfiles[0].Email = "test@example.com"

我知道我想要像这样 Put 该实体:

  1. k = datastore.Put(c, k, userProfiles[0])

如何从 userProfiles[0] 获取初始键以便调用 Put

英文:

I have a model:

  1. type UserProfile struct {
  2. UserID string `datastore:"user_id" json:"user_id,omitempty"`
  3. Username string `datastore:"username" json:"username,omitempty"`
  4. StripUsername string `datastore:"strip_username" json:"strip_username,omitempty"`
  5. Email string `datastore:"email" json:"email,omitempty"`
  6. LoginType string `datastore:"login_type" json:"login_type,omitempty"`
  7. CurrentSession string `datastore:"current_session" json:"current_session,omitempty"`
  8. FBAcessToken string `datastore:"fb_access_token" json:"fb_access_token,omitempty"`
  9. Created time.Time `datastore:"created" json:"-"`
  10. }

And I perform a .GetAll to populate it:

  1. // Skip a few steps here
  2. var userProfiles []UserProfile
  3. q.GetAll(c, &userProfiles)

Say I want to modify one of those entities:

  1. userProfile[0].Email = "test@example.com"

I know I want to Put that entity like so:

  1. k = datastore.Put(c, k, userProfile[0])

How do I get that initial key from userProfile[0] to call Put with?

答案1

得分: 3

GetAll 返回的是键(keys):

  1. var userProfiles []UserProfile
  2. keys, err := q.GetAll(c, &userProfiles)
  3. if err != nil {
  4. // 处理错误
  5. }

使用从 GetAll 返回的键来更新实体(entities):

  1. userProfile[0].Email = "test@example.com"
  2. _, err = datastore.Put(c, keys[0], userProfile[0])
  3. if err != nil {
  4. // 处理错误
  5. }
英文:

GetAll returns the keys:

  1. var userProfiles []UserProfile
  2. keys, err := q.GetAll(c, &userProfiles)
  3. if err != nil {
  4. // handle error
  5. }

Update entities using the keys returned from GetAll:

  1. userProfile[0].Email = "test@example.com"
  2. _, err = datastore.Put(c, keys[0], userProfile[0])
  3. if err != nil {
  4. // handle error
  5. }

huangapple
  • 本文由 发表于 2015年3月16日 10:26:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/29068749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定