I’m building a simple framework and hoping it will help me better understand OOP. Well, I’ve come across my first hurdle.
I’m naturally using MVC pattern and I have a user model. I then have a ‘table’ which manages the collection of user objects. I have an abstract model and table. Similar to Zend Framework layout.
Anyway, I’m now wondering what is the best way to execute a query? I have a database object which manages the database layer. Currently using mysqli object.
Trouble is my connection is a member of my application object, how do I access that from within my table object to execute a query? I obviously can’t create a new object because that will connect to the database again. I need to reuse this same db object.
Any ideas? I understand if this isn’t optimal design. Right now I’m just looking for advice.
Edit: Should I just use Doctrine?
doctrine or not?
If your intention is to learn how to write SQL Queries and to create something for your own, use mysqli or PDO.
If you would like to learn how to use a real object oriented database layer, use doctrine. With 3 pages tutorial, you can do the most general things. However some very special cases are better coded in raw sql than with doctrine. But if you know how to do things with doctrine it is a very useful library and you end up writing better, consistenter, more readabler and extensiabler (:-)) code.