Jump to content

Mysqli fetch_object with class name stored in table


Fawn

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.