public static function toDBArray($metadata, $include_title = true, $include_missing = true, &$types = array()) {
    $fields = array();

    if (!is_array($metadata)) {
      $metadata = (array) $metadata;
    }

    foreach (self::$fields as $key => $config) {

      // Ignore title?
      if ($key === 'title' && !$include_title) {
        continue;
      }

      $exists = array_key_exists($key, $metadata);

      // Don't include missing fields
      if (!$include_missing && !$exists) {
        continue;
      }

      $value = $exists ? $metadata[$key] : null;

      // lowerCamelCase to snake_case
      $db_field_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key));

      switch ($config['type']) {
        case 'text':
          if ($value !== null && strlen($value) > $config['maxLength']) {
            $value = mb_substr($value, 0, $config['maxLength']);
          }
          $types[] = '%s';
          break;

        case 'int':
          $value = ($value !== null) ? intval($value) : null;
          $types[] = '%d';
          break;

        case 'json':
          $value = ($value !== null) ? json_encode($value) : null;
          $types[] = '%s';
          break;
      }

      $fields[$db_field_name] = $value;
    }

    return $fields;
  }