Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6667411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:55:49+00:00 2026-05-26T02:55:49+00:00

I have MyModel that, whenever it is created or updated, I want to insert

  • 0

I have MyModel that, whenever it is created or updated, I want to insert a row into my_model_changes (MyModelChange) that is a log of all changes.

The problem is that I need MyModelChange to be able to save the current user’s ID, so that it stores who authorised the change to MyModel.

I know the model shouldn’t be able to access the Auth session in MVC. However, every change to MyModel must be logged, and so I don’t want to leave it up to people to remember to do this in a controller every time they modify the model, because there is a risk it will be forgotten or not done properly, in which case the log won’t be complete. Instead, I want to use MyModel::afterSave() so that it is automated and will always happen properly.

So how do I get MyModel to find the Auth user’s ID?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:55:50+00:00Added an answer on May 26, 2026 at 2:55 am

    Maybe my solution is not the most elegant way but I used it in my app and it worked. Before saving in the controller I extend $this->data with the user_id like this:

    $this->data['MyModel']['user_id']=$this->Auth->user('id');
    

    so I can use it in my model afterSave method. I hope it could help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data coming into my Model, how do I setup to insert the
I have a table with data that needs to be updated at run-time by
I have an array of objects that I want to save as a file
I have the requirement that whenever there is a model get's added/changed/deleted, it should
I have a model that contains a collection, such as this: class MyModel {
I have partial view that displays model-specific flash messages. The partial looks like: app/views/mymodel/_flashpartial.erb
I currently have 2 queries that are returning lists of MyModel like this: var
Currently I have my entities working with models that look like: public class MyModel
I have a dropdown as follows: @Html.DropDownList(studentList, myModel.GetMyList()) I have a variable that stores
I have an object model that contains a boolean field MyBool. I want populate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.