Fawn Posted August 14, 2012 Share Posted August 14, 2012 I'm saving the forms of my website in a MySQL database, with a separate table containing the form inputs. The table with inputs has the field `'type'`, which contains the type of an input, e.g. checkbox, text, select, password, etc. (matching HTML input types) Table Inputs ------------ id form_id name default_val required type The inputs are also represented in the model of my framework by a generic input class containing generic info (name/default value/is required/..) extended by child classes for each type of input. Each child class has specific characteristics, so it's worth the trouble of keeping them separate and not merging them into the generic class. Input | |-- Checkbox extends Input |-- Select extends Input |-- Password extends Input Currently, I'm using `mysqli->fetch_object` to retrieve the inputs from the database and create generic input objects. The generic object can't be cast to a child object (user defined objects can not be cast), so that won't work. Right now I'm using a special constructor in each child input class, to copy the values of the generic input object I pass as a parameter. This doesn't feel right, it doesn't seem to me like a good OOP practice. My problem is: I want to use a single database table for all the inputs, but in the model I want to treat each type of input as a different class/object. The `class_name` is saved in the table, hence it is available right after calling `fetch_object` for which you need it. How can I retrieve my input objects from the database in the correct class type? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 14, 2012 Share Posted August 14, 2012 I think the most prudent question is "why?" in this case, as in why do you want to use different classes for each of the inputs? A class is meant to keep related data and processing together, and if they're not related why keep them in the same table? Thus we need to know a bit more about your reasoning behind this, as I suspect you're following a sub optimal course on this. Quote Link to comment Share on other sites More sharing options...
Fawn Posted August 14, 2012 Author Share Posted August 14, 2012 The different form objects need the same basic information stored in the database table, but each of them has a very different behaviour. Next to the completely different validate methods (checking if an acceptable input value has been provided), I got some other additions: The checkbox contains methods to determine if it should be checked, based on post/db value The textarea object contains extra info regarding dimensions and whether it should be turned into a rich-text editor. The select input contains option values, next to methods regarding the determination of a selected option. I could write a big input class, handling all kinds of situations differently based on booleans and type checks, but that doesn't seem clean OOP to me. Also, I could create a table for each different input, but apart from the (separate) options table, the inputs are represented very similar in the database. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 14, 2012 Share Posted August 14, 2012 In that case I'd write a different validate method for each of the input types, which you'd need to do anyway, one method for saving them in the database, and one method for retrieving and validating them after POST. However, it's all quite abstract at the moment, so I think it would be quite beneficial if you could ground your case with some specific examples. That way we can see more clearly what you're trying to do, and thus either provide you with a better way or give some hints on how to proceed with what you want to do. Remember, the more information we have, the easier it'll be for us to help you. 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.