Select json more.
@param  string|array $field
@param  string       $as
@param  string       $type
@return self
551034
public function selectJsonMore($field, string $as, string $type = 'object'): self
    {
        return $this->selectJson($field, $as, $type, false);
    }
559459
public function addChange(
        string $field,
        $value,
        int $type = self::TYPE_VALUE
    ): self {
        $this->changes[] = [
            'field' => $field,
            'type' => $type,
            'value' => $value,
        ];

        return $this;
    }
340108
public function select($fields=null)
    {
        if (func_num_args() > 1) {
            $fields = func_get_args();
        }
        $this->getQuery()->type('select')->fields($fields);
        return $this;
    }
442619
public function setFilterSelectAjax(
        string $field,
        array $array,
        array $selected,
        string $url,
        string $class = '',
        string $style = '',
        string $placeholder = ''
    ): ColumnInterface {
        return $this->setFilter($field, $array, self::FILTER_TYPE_SELECT_AJAX, $selected, $class, $style, $placeholder, $url);
    }
954177
public function select($fields = [])
    {
        if (is_array($fields)) {
            foreach ($fields as $field) {
                $this->addSelect($field);
            }
        } elseif (is_string($fields)) {
            $this->addSelect($fields);
        }

        return $this;
    }
26574
public function select($fields)
    {
        $this->select = array_merge(
            $this->select,
            Arr::wrap($fields)
        );

        return $this;
    }
121919
public function right($field = 'right', $type = 'integer', callable $callback = null)
    {
        $this->validateNumericField($type, $field);

        $this->mapField($type, $field, $callback);

        $this->right = $field;

        return $this;
    }
1069878
protected function manageFieldDatatableJson($entity, $field, $reversedField, $type = 'one')
    {
        if ($entity == null || $field == null || $this->has($this->route_name.'_'.$field.'_datatable') == false) {
            throw new \Exception('All the parameters are not correctly set');
        }

        $postDatatable = $this->get($this->route_name.'_'.$field.'_datatable');
        $postDatatable->buildDatatable(['entity' => $entity]);

        if (method_exists($postDatatable, 'addLineFormatter') && method_exists($entity, 'getId')) {
            $postDatatable->addLineFormatter(function ($ligne) use ($entity) {
                $ligne['entity_id'] = $entity->getId();

                return $ligne;
            });
        }

        $datatable = $this->get('sg_datatables.query')->getQueryFrom($postDatatable);

        $entityName = $this->entity_name;

        if ($type === 'one') {
            $datatable->addWhereAll(function (QueryBuilder $qb) use ($entity, $entityName, $reversedField) {
                $qb
                    ->andWhere($qb->getRootAliases()[0].'.'.$reversedField." = :$entityName")
                    ->setParameter("$entityName", $entity)
                ;
            });
        } elseif ($type === 'many') {
            $datatable->addWhereAll(function (QueryBuilder $qb) use ($entity, $entityName, $reversedField) {
                $qb
                    ->join($qb->getRootAliases()[0].'.'.$reversedField, 'reverseField')
                    ->andWhere('reverseField.id = :'.$entityName.'_id')
                    ->setParameter($entityName.'_id', $entity->getId())
                ;
            });
        }

        return $datatable->getResponse();
    }
908980
public function getJsonFieldType($field)
    {
        return isset($this->options['field_type'][$field]) ? $this->options['field_type'][$field] : null;
    }
365706
private static function setAttr(array $attributes, $field, $name, $type)
    {
        $attrs = '';

        if (in_array($type, array_keys(self::$preAttrs))) {
            $attributes = self::$preAttrs[$type] + $attributes;
        }

        if ($name !== null) {
            $attributes = array( 'name' => $name ) + $attributes;
        }

        if ($type !== 'select' && $type !== 'textarea' && $type !== 'form') {
            $attributes = array( 'type' => $type ) + $attributes;
        }

        foreach ($attributes as $key => $val) {
            $attrs .= ' ' . $key . '="' . self::entities($val) . '"';
        }

        return str_replace('{{attr}}', $attrs, $field);
    }
662809
public function type($field)
    {
        if (!is_array($this->_context['schema'])) {
            return null;
        }

        $schema = Hash::get($this->_context['schema'], $field);
        if ($schema === null) {
            $schema = Hash::get($this->_context['schema'], $this->stripNesting($field));
        }

        return isset($schema['type']) ? $schema['type'] : null;
    }
47876
protected function update($type, $field, $value)
    {
        $this->update[$field] = [$type, $value];

        return $this;
    }
490251
public function selectSub($query, $as)
    {
        $this->handler->setBuilder($query)
            ->collectSubQueryTags();

        return parent::selectSub($query, $as);
    }
777013
public function selectJson($field, string $as, string $type = 'object', bool $reset = true): self
    {
        if (!in_array($type, ['object', 'array'])) {
            throw new BuilderException("Given JSON type '{$type}' is not implemented!");
        }

        $reset && $this->reset();

        static $server, $serverVersion, $serverVersionMin, $fnJsonObject, $fnJsonArray, $toField, $toJson;

        if ($server == null) {
            if ($this->agent->isMysql()) {
                $server = 'MySQL'; $serverVersionMin = '5.7.8';
                $fnJsonObject = 'json_object'; $fnJsonArray = 'json_array';
            } elseif ($this->agent->isPgsql()) {
                $server = 'PostgreSQL'; $serverVersionMin = '9.4';
                $fnJsonObject = 'json_build_object'; $fnJsonArray = 'json_build_array';
            }

            $serverVersion = $this->link->getDatabase()->getInfo('serverVersion');
            if (version_compare($serverVersion, $serverVersionMin) == -1) {
                throw new BuilderException(sprintf('JSON not supported by %s/v%s, minimum v%s required',
                    $server, $serverVersion, $serverVersionMin));
            }

            $toField = function ($field) {
                switch ($field) {
                    case 'null':
                        return null;
                    case 'true':
                    case 'false':
                        return ($field == 'true') ? true : false;
                    default:
                        if (is_numeric($field)) {
                            $field = strpos($field, '.') === false ? intval($field) : floatval($field);
                        } elseif ($field && $field[0] == '@') {
                            $field = $this->agent->escapeIdentifier($field);
                        } else {
                            $field = $this->agent->escape($field);
                        }
                }
                return $field;
            };

            $toJson = function ($values) use (&$toJson, &$toField, $fnJsonArray, $fnJsonObject) {
                $json = [];
                foreach ($values as $key => $value) {
                    $keyType = gettype($key);
                    $valueType = gettype($value);
                    if ($valueType == 'array') {
                        // eg: 'bar' => ['baz' => ['a', ['b' => ['c:d'], ...]]]
                        $json[] = is_string($key) ? $this->agent->quote(trim($key)) .', '. $toJson($value)
                            : $toJson($value);
                    } elseif ($keyType == 'integer') {
                        // eg: ['uid: @u.id' or 'uid' => '@u.id', 'user' => ['id: @u.id' or 'id' => '@u.id', ...], ...]
                        if ($valueType == 'string' && strpbrk($value, ',:')) {
                            if (strpos($value, ',')) {
                                $json[] = $toJson(Util::split(',', $value));
                            } elseif (strpos($value, ':')) {
                                [$key, $value] = Util::split(':', $value, 2);
                                if (!isset($key, $value)) {
                                    throw new BuilderException('Field name and value must be given fo JSON objects!');
                                }

                                if (!isset($json[0])) {
                                    $json[0] = $fnJsonObject; // tick
                                }

                                $json[] = $this->agent->quote(trim($key)) .', '. $toField($value);
                            }
                        } else {
                            // eg: ['u.id', '@u.name', 1, 2, 3, true, ...]
                            if ($valueType == 'integer') {
                                $value = $toField($value);
                            }

                            if (!isset($json[0])) {
                                $json[0] = $fnJsonArray; // tick
                            }

                            $json[] = $value;
                        }
                    } elseif ($keyType == 'string') {
                        // eg: ['uid' => '@u.id']
                        if (!isset($json[0])) {
                            $json[0] = $fnJsonObject; // tick
                        }

                        $json[] = $this->agent->quote(trim($key)) .', '. $toField($value);
                    }
                }

                if ($json) {
                    $fn = array_shift($json);
                    $json = $fn ? $fn .'('. join(', ', $json) .')' : '';
                    if (substr($json, -2) == '()') { // .. :(
                        $json = substr($json, 0, -2);
                    }
                    return $json;
                }

                return null;
            };
        }

        $json = [];
        $jsonJoins = false;
        if (is_string($field)) {
            foreach (Util::split(',', $field) as $field) {
                if ($type == 'object') {
                    // eg: 'id: @id, ...'
                    [$key, $value] = Util::split(':', $field, 2);
                    if (!isset($key, $value)) {
                        throw new BuilderException('Field name and value must be given fo JSON objects!');
                    }
                    $json[] = $this->agent->quote(trim($key));
                    $json[] = $toField($value);
                } elseif ($type == 'array') {
                    // eg: 'id, ...'
                    $json[] = $toField($field);
                }
            }
        } elseif (is_array($field)) {
            $keyIndex = 0;
            foreach ($field as $key => $value) {
                $keyType = gettype($key);
                if ($type == 'object') {
                    // eg: ['id' => '@id', ... or 0 => 'id: @id, ...', ...]
                    if ($keyType == 'integer') {
                        $value = Util::split(',', $value);
                    } elseif ($keyType != 'string') {
                        throw new BuilderException("Field name must be string, {$keyType} given!");
                    }

                    if (is_array($value)) {
                        if ($keyType == 'string') {
                            // eg: ['id' => '@id', ...]
                            $key = $this->agent->quote(trim($key));
                            $json[$keyIndex][$key] = $toJson($value);
                        } else {
                            // eg: [0 => 'id: @id, ...', ...]
                            $value = $toJson($value);
                            $value = preg_replace('~json(?:_build)?_(?:object|array)\((.+)\)$~', '\1', $value); // :(
                            $json[$keyIndex][] = [$value];
                        }
                        $jsonJoins = true;
                        continue;
                    } elseif (is_string($value)) {
                        $key = $this->agent->quote(trim($key));
                        $json[$keyIndex][$key] = $toJson(Util::split(',', $value));
                        $jsonJoins = true;
                        continue;
                    }

                    $json[] = $key .', '. $toField($value);
                } elseif ($type == 'array') {
                    // eg: ['@id', '@name', ...]
                    if ($keyType != 'integer') {
                        throw new BuilderException("Field name must be int, {$keyType} given!");
                    }
                    $json[] = $toField($value);
                }
                $keyIndex++;
            }
        } else {
            throw new BuilderException(sprintf('String and array fields accepted only, %s given!',
                gettype($field)));
        }

        $as = $this->agent->quoteField($as);
        $fn = ($type == 'object') ? $fnJsonObject : $fnJsonArray;

        if ($jsonJoins) {
            $jsonJoins = [];
            foreach ($json[0] as $key => $value) {
                $jsonJoins[] = is_array($value) ? join(', ', $value) : $key .', '. $value;
            }
            $json = $jsonJoins;
        }

        return $this->push('select', sprintf('%s(%s) AS %s', $fn, join(', ', $json), $as));
    }
1147917
public function select(string $fields): QueryBuilderInterface
	{
		// Split fields by comma
		$fieldsArray = explode(',', $fields);
		$fieldsArray = array_map('mb_trim', $fieldsArray);

		// Split on 'As'
		foreach ($fieldsArray as $key => $field)
		{
			if (stripos($field, 'as') !== FALSE)
			{
				$fieldsArray[$key] = preg_split('` as `i', $field);
				$fieldsArray[$key] = array_map('mb_trim', $fieldsArray[$key]);
			}
		}

		// Quote the identifiers
		$safeArray = $this->driver->quoteIdent($fieldsArray);

		unset($fieldsArray);

		// Join the strings back together
		for($i = 0, $c = count($safeArray); $i < $c; $i++)
		{
			if (\is_array($safeArray[$i]))
			{
				$safeArray[$i] = implode(' AS ', $safeArray[$i]);
			}
		}

		$this->state->appendSelectString(implode(', ', $safeArray));

		return $this;
	}
354366
function select (object, fields) {
  return arrayify(fields).reduce(function (prev, curr) {
    prev[curr] = object[curr]
    return prev
  }, {})
}
409179
protected function parseJson($type, \SimpleXMLElement $json)
    {
        if (!isset($json->$type)) {
            return false;
        }
        foreach ($json->$type as $url) {
            $this->addArray($type, (array)$url);
        }
        return true;
    }