To state the usual design: When the user logs out, the session is dropped and the cart info is saved to the db. When the user logs in, the db data is read back in and the session data is populated with it. When the user makes an adjustment to the cart this means an update of the session data and the db.
To state what is different in your approach: The session is saved to a session db table in addition to the user table. Am I right this far? Are you using Cake's functionality to store session data in the db or do you use a 'manual' approach with precisely another session table? In general, I would suggest you keep the persistent data in one place (user's table in your case). The session data is then just like a temporary image for quick access to the cart data without re-reading from the persistent (non-temporary) data store - I would use the built- in session mechanism for that. With the built-in session mechanism, the session can be quickly updated on every change and on login inside the controller. On logout there is no need to store session data because the session will be invalid afterwards. To update the cart in the user table, I would add utility methods to do that inside the User model class if the call to $this->User->save etc. is too bulky in the controller. You could also add a method to your model like this: saveCart($data), and the return value could be an updated cart array which you would just need to put into your session, or you pass the session array by reference. If you insist on having your own model for session data, then an update to the cart data in the user model should also trigger an update to the session model, which is possible if you define the associations correctly (e.g. User has one Session), then use $this- >Session in the User model. All in all I think you only need the cart data once in the and let cake handle the session. In this case however I don't see why you need to merge session data on login. It's already saved on the users table and a new session should be created on login. Best, JP On Jan 31, 3:25 am, kiger <[EMAIL PROTECTED]> wrote: > I have a shopping cart. I store the data for it in the Session table > and Users table. Why both? Well, when a user logs out or leaves the > site and his session expires, I want his cart to still be saved. So I > save the cart to both the session and users table upon a modification > and on logging out. Then when the user logs in, I merge any saved cart > in the users table with the cart in the session. > > Why not create a separate Carts table? I have my reasons, but mainly > because I don't want to be bothered finding a way to update the > session_id in a Carts table when it changes. Other reasons too... > > So what is my design question? Well, I would like to move the junk in > the UsersController relating to the cart out of that controller and > put it somewhere else more relevant. More specifically, the code > saving the cart on logout and the code merging the carts on login. > > The problem I am running into is how to move it out... > [1] I could move it to a component (ex., CartComponent) but cake > doesn't want me accessing models from components (e.g., the User > model). > [2] I could move it to a model (ex., CartModel) but cake doesn't want > me accessing the session from models and because there is no > corresponding table, I could not access the necessary Users table. > [3] I could move it to the CartsController, but then I have to bite > the performance hit by calling requestAction (also, this action has > changed so any call to it creates a new session instance which costs 2 > database hits, ouch). > > I know that there are ways to load model instances, and I know there > are ways to do what cake doesn't want you to do. But this is a simple, > or should I say common, feature in design and it shouldn't be so > difficult to find a solution for... > > So what do you guys think? Is there a way to successfully move this > code out while simultaneously respecting cake's design goals? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
