1

I'm trying to modelize a configuration table for my application, and I'd like to have your opinion on how to store multiple values for a configuration value.

For example, how to store the available languages? How to store the allowed files extensions, etc.?

I'd like to have a field key and a field value in my table configuration, but I have multiple ways of storing the value:

  • Serializing a PHP array and storing this in one row,
  • Storing all values separated by a comma, like this 1,4,9,42
  • Having multiple rows with the same key but different values. I don't see how I could get this working, it would be very unusable...

Thanks for your help :)

2
  • Where will you store the database configuration? Did you consider not storing application config in the DB? Commented Dec 13, 2014 at 22:54
  • I meant to store my website configuration in my database, under a table named 'configuration'. Sorry if this was unclear. Commented Dec 13, 2014 at 22:56

2 Answers 2

4

Leaving aside any discussion of whether or not this is a good idea, many people do this. A very common approach is to have a 'blob' field on a 'configuration' table and store your data in JSON format. You'll serialize and deserialize yourself but that's no big deal.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to store your configuration in database (it's not recommended, but it makes sense if you have many instances of your application) - let's analyze it:

  1. Approach with storing multiple rows with same key - let's forget about it ;)

  2. Storing many values separated by , - that one would make some problems if you'll need to store value with coma (for example: 10,000 as ten thousand). Then you'll need to modify it to store values like in CSV file encapsulated with ".

  3. Best idea - storing serialized PHP values. As your app is written in PHP that would be the best native method. Using this approach would be as easy as:

    $configValue=unserialize(getMyValueFromDb("myKey")); //or saving it after change $configValue="new value"; saveMyValueToDb(serialize($configValue));

Also - PHP serialize and unserialize take care about escaping values and convert it properly so you don't need to worry about some coma-separated-string wrongly split with explode.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.