Submit form on select option change in cakephp 4.4

I saw that using $session = $this->request->getSession(); is creating a conflict with the users, when I deleted this line it begun to work fine, but for some reason is giving me the exception Record not found in table “users”, how can I solve this issue?
And the reason that I was using the session is to save the profile_id for all the site, I need that variable to check the acl permissions, is there another way to store the profile_id and make it available for all the site?

I’m pretty sure that $session->consume('profile_id'); is going to remove profile_id from your session. Is that your intent? Or did you mean $session->read('profile_id');? Are you doing the same somewhere else with your user ID? Maybe that’s why you’re getting the wrong value in this function?

Have you looked at what value you are getting in $user_id? If it’s null, your session is either losing it somewhere or else you’re not putting it in there in the first place. If it’s something else, you may be setting it incorrectly. I don’t think we can identify any such errors from the code you’ve shown so far, it’s probably elsewhere. (This isn’t a request to post all your code, it’s a suggestion of how to debug it on your own.)

Did you notice my previous comment about “Well, I guess you’d want to make sure that the session cookie is being sent through your Ajax request, and do some debugging to figure out if the session, etc. is being correctly initialized in this particular use case.” ?

I solved it, now I have $session->read('profile_id') and is working perfect, the problem with record not found in table users was that I wanted to get the user with a profile_id that was not in users, the profile_id I was getting was in another table that was not related to users, now I separated the query, in one part I have $this->Users->get($user_id) and in another I have $this->Users->Profiles->get($profile_id) and is working fine, thank you very much.