Git Product home page Git Product logo

Comments (5)

jinzhu avatar jinzhu commented on April 28, 2024

Hi @leebrooks0

No, this is not supported, but under consideration.

But even if gorm supported it, how will you use it for created_by_user_id?

from gorm.

leebrooks0 avatar leebrooks0 commented on April 28, 2024

I intend using Revel, which has cookie based sessions, so once the user has logged in I would save their user id in the session and then fetch it when needed.

from gorm.

jinzhu avatar jinzhu commented on April 28, 2024

But because you can't pass any argument to callbacks, so how to get the user id from session?

So maybe better if gorm support setters/getters like this, any suggestions?

// model
type User struct {
    UpdatedBy string
}

// define callbacks
func SaveUpdatedBy(tx *gorm.DB, obj interface{}) (err error) {
    updated_by := reflect.ValueOf(obj).Elem().FieldByName("UpdatedBy")
    if updated_by.IsValid() {
        updated_by.Set(reflect.ValueOf(tx.Get("current_user_id")))
    }
    return
}

DB.RegisterCallback("BeforeSave", SaveUpdatedBy)

// controller
db := DB.Set("current_user_id", 20) // 20 is the value from session
db.Save(&user)

from gorm.

leebrooks0 avatar leebrooks0 commented on April 28, 2024

I see, so User.UpdatedBy needs to be manually set. It seems like these things just need to be handled on a case by case basis. If User.UpdatedBy was "Fred" and then "John" makes the update, then unless you manually reset User.UpdatedBy to "John" then on save it is still going to say "Fred" made the update.

from gorm.

jinzhu avatar jinzhu commented on April 28, 2024

I am planing a plugin system for gorm, hope this could resolve your problem, will notice you when it is done.

from gorm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.