I require the ability to store, modify and retrieve YAML data content in a MySQL database.
My project (and question) aims:
- Parse .yml files into a PHP multi-dimensional array
- Store these into a MySQL database
- Allow modification of individual values within the database primary aim
- Retrieve the values from the database, parse, and place back into a .yml file
Step 1 – An Example YAML file
string 'name:
singular: null
plural: null
fields:
price:
label: Preis
company_id:
label: null
placeholder: null'
The important things to note here are that there could be an unlimited number of nested keys, and also keys and values may have the same name / data.
Step 2 – Place this into a PHP array
I’m using a library called Spyc to parse the YAML. This works great! It gives me the following:
array
'name' =>
array
'singular' => null
'plural' => null
'fields' =>
array
'price' =>
array
'label' => string 'Preis' (length=5)
'company_id' =>
array
'label' => null
'placeholder' => null
My aim next is to store each of these in a MySQL database. So…
Step 3 – CSV them
Here I have written my own function, which is basically a long list of foreach()s to separate these details into the following array:
array
0 => string 'name||singular||' (length=16)
1 => string 'name||plural||' (length=14)
2 => string 'fields||price||label||Preis' (length=27)
3 => string 'fields||company_id||label||' (length=27)
4 => string 'fields||company_id||placeholder||' (length=33)
Step 4 – MySQL DB
Logically, I’m trying to store these strings in a database. I’m not sure of the schema. I’m guessing value will be the end($array[$key] in a value column, and the key column will contain the rest of the elements in the array.
This will effectively allow me to change values within the database before pulling them back out, parsing and returning to the .yml file.
Step 5 – I’m LOST!
So I’m not sure what to do now. Currently I’m trying to parse these strings with the following logic:
- The last value
end($array[$key]is the value, and all previous elements in the array are the parent keys - Once I’ve gotten that code working, effectively turning those strings back into an array would be the next priority
There’s got to be a better way to do this. My main aim is to be able to update the individual values in the database. My main problem is the fact that the array dimension count is not constant.
How I can be helped
Please offer me:
- More appropriate methods of achieving my project aims (see top) if there are any
- The best way to store this data in the database allowing easy modification of values (for example the
nulls you see in Step 1) - Upon returning this data from the database, a PHP function to return these keys and values to the exact structure of the original array (Step 2) so that I can use Spyc to convert this back to YAML and output to the .yml file.
If you want to create a key-value store that can be serialized as YAML, you should probably just create that:
In this case,
idis how you refer to the root record,parent_idis a mechanism for linking in child records,typeis used to distinguish between an array map and an indexed array.keyandvalueare used to store the respective values.With this you should be able to create the appropriate records.
An example from your data might be to create the root node:
Then add in the values for the
namekey presumingidwas 1 for the previousINSERT:The process repeats recursively for each value:
In any case, you’ll need to write a wrapper class for all this or it’ll be too complicated to use.
In practice I’m not sure going through all of this trouble is going to be much easier than simply storing the YAML as-is inside a
LONGTEXTfield and calling it a day. Manipulating and re-saving is usually not that expensive. Iterating over a multi-tiered tree structure is.You’ll have to be careful to avoid stomping writes in race conditions for the pure YAML approach, though, but that’s nothing that can’t be solved using a
revisioncolumn that tracks which version you’re saving against. Construct yourUPDATEso that it won’t match:If some other process had updated it already you would see the query failed to run and could handle it accordingly.