Sometimes you may need to store complicated data with non-fixed fields/data types in table. There are different ways for that but if we use symfony where YML is so native and good known – why dont store data in table as YML-formed value.
It’s pretty straightforward with using of Spyc class and its methods YAMLLoad, YAMLDump to convert YAML from/into array but some things worth mentioning:
YML usually contains more information that may fit simple varchar so you may want to use “longvarchar” type for storing this kind of info and then I faced with the problem that in order to access this value from $data object you may need to use additionally getContents method, e.g. $data_object->getValue()->getContents().
The other thing – dont forget to decode your text before printing it with YAMLLoad by calling html_entity_decode method, so it finally looks like to dump YAML into PHP array:
Spyc::YAMLLoad(html_entity_decode($data_object->getValue()->getContents()))
and to store text field as YAML well-formed object use this:
Spyc::YAMLDump($this->getRequestParameter(‘some_value’))
The other non-ordinary thing is that you may want to prepare fixture YML files for batch processing, in this case the problem you may expect is how to prepare YAML file which contains text field with break lines. I found the solution in sfSimpleForumPlugin (see using content field in this sample):
post_12:
topic_id: topic_7
content: |
If you see no alert, it means that your output escaping is turned on
user_id: tarzanman
That’s it.