Accessing Session Variables in Model
This early morning (right now based on Indonesia time GMT+7, it's 8.18 AM), I've been preparing my to-do-list to be done later in my office associated with my countdown-project. It's all about to log every query executed by user action (create/ add, edit and delete) to database by using cakePHP. I've been spending a lot of time on searching the snippet or at least what-kind-of-function-I-must-used-to, and not find my need yet.Yesterday, since I've found the how-to, based on the link I've found on Stackoverflow and edited it to be like what I want (read : need), I still have to do a lil bit experiment.
It's like this title's post. Accessing session variables in Model. Is that possible? Come on,, it's just PHP. Anything possible.
So many ways to get the session variables when we are in Model. For example, importing the Session Component (or even the CakeSession core) or simply call $_SESSION. That's it. But, remember, we must know about 'BIG BUT' below..
Of course we could access session in Model BUT of course it's also breaking the MVC design pattern itself.
The solution
Callbacks. The controllers and model callback is created to avoid a broken the MVC pattern. So HOW? Refer to this link, I got what I need :) Aaah.. Stackoverflow is such a savior :D
Disclaimer
I'm not sure if my explanation is clear enough. Follow the reference-links below if you wanna get you're own understanding :D
References :
- http://stackoverflow.com/questions/2145688/cakephp-getting-session-variable-in-model-without-using-auth-component
- http://stackoverflow.com/questions/965815/cakephp-updating-a-session-variable-after-save
- http://cakephp.1045679.n5.nabble.com/Checking-user-session-in-model-td1294422.html
- https://groups.google.com/forum/?fromgroups#!topic/cake-php/FjvZtgqodXs
- The references pool : goo.gl/Da7wG
1 comments:
Write commentsNice
Reply