coxdabd Posted January 5, 2012 Share Posted January 5, 2012 Hi all, I'm struggling with a bit of logic here. What's the score on connection to a database inside of a class (PDO). I'm guessing it's bad practice and any queries should be passed as an array to the object - is this correct? What should I do with entering data into the DB? For example, say you have an orders class, one of the methods is to create the order, would you return an array from the method of all the data to be entered into the DB and then to enter back in the script? Getting pretty good at OOP, just need a little extra help. Have had a play with a framework (CodeIgnitor) but found it too limiting if that makes any sense - although I did like the MVC pattern. Quote Link to comment Share on other sites More sharing options...
dharmeshpat Posted January 5, 2012 Share Posted January 5, 2012 Can you try to clarify more about ur problem means are you telling that u have order form which user fills in and once it is fill u will create and array of all the data that user and filled in and will pass that array to the class function and insert it into the database than i say, It is bad practice to create a array after the data is submitted since the u will get the data in post as an array format and again creating new array not good practice Quote Link to comment Share on other sites More sharing options...
coxdabd Posted January 5, 2012 Author Share Posted January 5, 2012 Hey dude, apologies that was just an example - a bad one. What I'm trying to ask, is say you have an Order class for example, say one of the methods is to to view a particular order in the DB, would I put the SQL inside the method or feed the SQL to it through the script through a parameter - is connecting to a database inside a class bad practice? Quote Link to comment Share on other sites More sharing options...
trq Posted January 5, 2012 Share Posted January 5, 2012 If you are familiar enough with MVC you will understand that data is represented by Models. Your 'Order' class should represent a record from within your 'Orders' database table. It is fine for a Model such as this to depend on a 'Database' class. But this class should implement a well defined interface and should be passed to the Model via dependency injection of some variety. Quote Link to comment 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.