Skip to content

Instantly share code, notes, and snippets.

@Boorj
Last active December 12, 2015 08:47
Show Gist options
  • Select an option

  • Save Boorj/d2e3727bd54ffe90400c to your computer and use it in GitHub Desktop.

Select an option

Save Boorj/d2e3727bd54ffe90400c to your computer and use it in GitHub Desktop.
Convert select fields in bolt's contenttype
<?
/**
* ### convert_select
* Sometimes you set values as ['foo', 'bar'] and bolt stores them as strings in db.
* It works ok, if you set values as ['':'', 0: 'foo', 1: bar] in contenttypes.yml.
* I wanted to update my old values, so i created a util function. Here it is, use however you want.
*
* @param Application $app
* @param string $contenttype_slug
* @param array $allowed_fields - select fields that should be processed (for ex. ['vendors', 'models']).<br>
* Leave it = [] if you want all fields. [] by default
* @param bool|false $do_throw_exceptions - use that for dev mode if you want to break on possible errors.
*/
function convert_select ($app, $contenttype_slug, $allowed_fields = [], $do_throw_exceptions = false) {
// getting reverse mappings
$contenttype = $app['storage']->getContentType($contenttype_slug);
$fields = $contenttype['fields'];
$mappings = [];
foreach ($fields as $field_name => $field_data) {
if ($allowed_fields && !in_array($field_name, $allowed_fields))
continue;
if (empty($field_data['type'])) {
if ($do_throw_exceptions) throw new Exception("No type for field $field_name in $contenttype_slug");
else continue;
}
if ($field_data['type'] === 'select') {
if (empty($field_data['values'])) continue; // no values set
$keys = array_keys($field_data['values']);
$values = array_values($field_data['values']);
// checking if bolt returns the array: ['foo'=>'foo', 'bar'=>'bar']
$identical = true;
foreach ($keys as $i=>$key) {
if ($key !== $values[$i]) {
$identical = false;
break;
}
}
if ($identical){
if ($do_throw_exceptions) throw new Exception("$contenttype_slug / $field_name won't be processed. Values are set using incorrect format. Should be ['':'', '0': 'bar', '1' : 'foo', ...]");
else return;
}
$mappings[$field_name] = array_flip($field_data['values']); // use array_flip(array_values($field_data['values'])); if you want to convert
}
}
if (!$mappings){
if ($do_throw_exceptions) throw new Exception("Nothing to do: no 'select'-type fields in $contenttype_slug");
else return;
}
// getting records
$repo = $app['storage']->getRepository($contenttype_slug); /* @var Repository $repo */
$records = $repo->findAll();
if (!$records){
if ($do_throw_exceptions) throw new Exception("Nothing to do: no records in $contenttype_slug table.");
else return;
}
// main loop of replacements
foreach ($records as $record) {
/* @var Entity $record*/
foreach ($mappings as $field_name => $field_mapping) {
$vals = @json_decode($record->__get($field_name));
if (json_last_error() !== JSON_ERROR_NONE){ // some error occured = skipping
if ($do_throw_exceptions) throw new Exception( "Wrong values in $field_name in $contenttype_slug. Id = ". $record->getId());
else continue;
}
if ($vals) {
$new_vals = [];
foreach ($vals as $val) {
if (isset($field_mapping[(string)$val])){
$new_vals[] = $field_mapping[(string)$val];
}
}
$record->__set($field_name, json_encode($new_vals));
}
}
$repo->save($record);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment