proggR Posted August 12, 2011 Share Posted August 12, 2011 Currently my models all derive from a base Model class which instantiates the database adapter when it gets instantiated. This works just fine but the problem I have with it is that each instance of the model has its own reference to the database adapter. Even making it static, the models will all have a reference to the adapter, it'll just be the same one. I was thinking about loading the database adapter at the application level and passing it into the model when it gets instantiated but this doesn't really seem to fix the issue since each instance of the model will still have a reference to the adapter. This may not be a big deal at all but it is definitely a lot more ugly when I print_r the model. Any suggestions on ways around this? I'm not completely familiar how other frameworks handle this so I'm not sure where they're handling their database. **edit** Apparently Zend uses a mapper to map the model to the database. I'll ponder that for a bit and see if that's something I can do as well. I'd still appreciate any feedback if anyone feels obliged. Quote Link to comment https://forums.phpfreaks.com/topic/244609-databases-and-models/ Share on other sites More sharing options...
ignace Posted August 12, 2011 Share Posted August 12, 2011 The reason being that you shouldn't have a Model base class to begin with... The models that require a database connection can retrieve/make one when needed and those that don't, just don't. class User { public function __construct(Zend_Db_Adapter_Abstract $adapter) { $this->adapter = $adapter; } } class ContactEmail { public function __construct($charset = 'UTF-8') { $this->emailer = new Zend_Mail($charset); } } 2 model's and only tied to the components they actually need. Quote Link to comment https://forums.phpfreaks.com/topic/244609-databases-and-models/#findComment-1256489 Share on other sites More sharing options...
proggR Posted August 15, 2011 Author Share Posted August 15, 2011 I noticed that since all the models I have need access to the database so rather than including the same code in all of them I could just pull that into a class they all derive from. After thinking more about it using a Mapper makes a lot more sense so I'll be changing my code to use that approach. Quote Link to comment https://forums.phpfreaks.com/topic/244609-databases-and-models/#findComment-1257768 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.