Zend Framework application design - should session variables be accessed in the Model layer?

It depends. The way I think about this is such: A Model represents your data layer. Most of the time that data layer will be DB Table based The Session is just another data storage medium.

Conclusion: If the data that your model represents is stored in the Session, than it is OK to access that data from within the model An example is a Session based shopping cart. My cart's objects are models of my session data.

Fair enough. But I would still add a getter/setter for the session object so I can mock out later. – David Weinraub Sep 3 at 5:23 The issue that I'm seeing in this app (and hence the original motivation) is that #3 is NOT #2, or more cohrerently, not persisted to db and only to php's session mechanism.In effect you're getting dangling references.

In this case would seem to make more sense to either run a way that these specific methods won't be called unless there is a sess->user_id variable (ie done ar controller level) OR manage via the models constructor / initialization whether the user has a sess->user_id. In terms of issues of testing and cli (both of which are used), I could create manual sess values. – timpone Sep 3 at 16:36 @timpone Maybe I don't understand what you mean by dangling references.

A dangling reference issue would only happen if within another persistent Model you stored a session id as a foreign reference - which is more of a storage / database design issue. I would not relate my persistent storage (Database) and temporary storage (Session) even though they are both of the data layer. What my response meant to illustrate, was that in some instances, If the temporary storage was being used to model data, that referencing the Session within the Model would be acceptable.

– Francis Von Awesomesauce Sep 5 at 1:22 I think you shouldn't access session objects from model. Also, models isn't simply "data layers". It's your business logic and need to be apart from your data sources, including session variables.

– Keyne Sep 8 at 18:45 @Keyne - I never stated that models were just data layers, the purpose of this question and my answer was very specific and was not intended as an Overview of MVC architecture. That said, I stated that accessing the session was acceptable given a specific situation. A situation that your comment did not refute.

Furthermore your understanding of "business logic" is lacking somewhat - it encompasses both calculations and data storage. Also, if you wish to weigh in on a question submit an answer! – Francis Von Awesomesauce Sep 8 at 20:20.

Controller shd do a check weather session exist or not before using the model which uses that session inside it .

I have seen this but it runs into the problem of models having other models. What if a model used within another model uses a session variable? Seems like writing a lot of code in the controller to manage what should in fact be in the controller.

More thinking out loud than disagreeing with you. – timpone Sep 3 at 16:01 @timpone when passing session as a parameter to the model you still have to do all the checks in controller . As the name says controller job is to provide control over logic which goes inside models .

– beginner Sep 4 at 2:02 +1 Agree, you don't need to check session in models, this is the plug-ins job. Probably you're missing something in your architecture. Otherwise, this is a Zend_Registry job.

– Keyne Sep 8 at 18:57.

Y/N', then they probably don't need to be part of the model layer. If, however, it's more complex than that, they are probably inextricably linked to your business model and should be treated as such. The examples at the bottom of the Zend Test documentation show how to test the full MVC using a login fuction.

Presumably you could do the same when testing models?

Thx for pointing that out; like everything I think it depends. In a complex app, it seems like a recipe for disaster. In general, I find I always want to be doing some cli processing of data either to scrub it and having real session variables complicates that.

Can it be done? Yes Should it be done? Not sure – timpone Sep 4 at 1:33.

No it shouldn't. The storage type, should be apart from your business logic. For example: I have one simple plug-in that perform the access check and put the user object on the registry.So, instead of access session, the model have access to the registry, which is well defined.

$User = Zend_Registry::get('User'); // User model object From the theoretical point of view, everything should be accessed through data mappers. In the future, if you change from session storage to something else, you'll need to update it just in one place. Your models do not need to know from where the data came from.

If you are taking more than one path to get your data, probably this will cause some problems when your application get large. The OOP and layered systems approach suggestion is to created specialized objects and layers and keep things simple preventing specific actions to be spread all over the code. But again, you do not need to change that unless you see advantages.

Keep in mind that sometimes refactoring is more efficient than try to predict everything.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions