@param array   $avail_fields
@param boolean $names_only Return only an array of names, otherwise return all properties

@return array
340485
public static function getPrimaryKeys($avail_fields, $names_only = false)
    {
        $keys = [];
        foreach ($avail_fields as $info) {
            if ($info->isPrimaryKey) {
                $keys[] = ($names_only ? $info->name : $info);
            }
        }

        return $keys;
    }
18191
private function getMainFields(array $fields): array
    {
        $main = [];
        foreach ($fields as $field) {
            if ($field instanceof ChildrenAssociationField) {
                continue;
            }

            if ($field->is(Deferred::class)) {
                continue;
            }

            if (!$field->is(PrimaryKey::class)) {
                $main[] = $field;
                continue;
            }

            if ($field instanceof FkField) {
                $main[] = $field;
            }
        }

        return $main;
    }
1280296
protected function getFieldValidationResults(
        array $fields,
        array $allowedFields = [],
        array $fieldResults = [],
        array $options = []
    ): array {
        foreach ($fields as $fieldName => $value) {
            if (!in_array($fieldName, $allowedFields)) {
                $fieldResults[$fieldName] = new ValidationResultBasic(
                    false,
                    BuildOptionCode::invoke($options, static::CODE_UNRECOGNIZED_FIELD),
                    ['message' => 'Unrecognized field received: (' . $fieldName . ')']
                );
            }
        }

        return $fieldResults;
    }
308965
private function normalizeFields($_fields)
    {
        $fields = [];
        foreach ($_fields as $key => $value) {
            if (is_array($value)) {
                $fields[$key] = $value;
            } else {
                $fields[$value] = [];
            }
        }
        return $fields;
    }
158731
public function field($name)
    {
        return isset($this->_properties['fields'][$name])?
            $this->_properties['fields'][$name]:null;
    }
604125
public function only($fields)
    {
        if ( count(func_get_args()) > 1 ) {
            $fields = func_get_args();
        }

        if ( !is_array($fields) ) {
            $fields = [$fields];
        }

        $result = [];
        foreach ($this->all() as $fieldName => $value) {
            if ( in_array($fieldName, $fields) ) {
                $result[ $fieldName ] = $value;
            }
        }

        return $result;
    }
120631
public static function getDescriptionByName($name)
    {
        $key = strtoupper($name);
        if (isset(self::$availableNameList[$key])) {
            return self::$availableNameList[$key];
        } else {
            return false;
        }
    }
1267304
public function getField($name)
    {
        return (isset($this->fields[$name])) ? $this->fields[$name] : false;
    }
1354307
public function getFields($table,$onlyField=true)
    {
        $this->sql = 'SHOW full columns FROM ' .$this->tablePre. $table;
        $stmt = $this->execute();
        $fields = array();
        $info = $stmt->fetchAll();
        if(!$onlyField) return $info;
        foreach ($info as $value) {
            $fields[] = $value['Field'];
        }
        return $fields;
    }
258712
public function getAttributes(array $attribute_names = [], array $types = [])
    {
        $attribute_map = [];

        if (empty($attribute_names)) {
            $attribute_map = $this->attribute_map->getItems();
        } else {
            foreach ($attribute_names as $attribute_name) {
                $attribute_map[$attribute_name] = $this->getAttribute($attribute_name);
            }
        }

        if (!empty($types)) {
            $attribute_map = array_filter(
                $attribute_map,
                function ($attribute) use ($types) {
                    return in_array(get_class($attribute), $types);
                }
            );
        }

        return new AttributeMap($attribute_map);
    }
426346
public function fields()
    {
        if (isset($this->cache['fields']))
        {
            $result = $this->cache['fields'];
        }
        else
        {
            $result = Eresus_CMS::getLegacyKernel()->db->fields($this->table);
            $this->cache['fields'] = $result;
        }
        return $result;
    }
713619
public static function getCodesByNames($names=array())
	{
		if (!($gdb = self::getGeoDBO()))
		{
			return array();
		}

		$names = array_map('strtolower', $names);
		foreach ($names as $k => $name)
		{
			$names[$k] = $gdb->quote($name);
		}

		$gdb->setQuery("SELECT DISTINCT name, code FROM countries WHERE LOWER(name) IN (" . implode(",", $names) . ")");
		$values = $gdb->loadAssocList('name');
		if (!is_array($values))
		{
			$values = array();
		}
		return $values;
	}
144556
public function select($name, $options = array(), $selected = null, array $attributes = array(), array $extra = array())
    {
        /**
         * Swap values so programmers can skip the $value argument
         * and pass the $attributes array directly.
         */
        if (is_array($selected) && empty($attributes)) {
            $extra = $attributes;
            $attributes = $selected;
            $selected = null;
        }

        return $this->doBuild('select', $name, $selected, $attributes, $extra, $options);
    }
1234694
public function build($fields)
    {
        $data = [];

        if (Cuztom::isArray($fields)) {
            foreach ($fields as $type => $args) {
                $args = Cuztom::merge($args, [
                    'metaBox'  => $this,
                    'metaType' => $this->metaType,
                    'object'   => $this->object,
                    'parent'   => $this->id,
                ]);

                $field = Field::create($args, $this->values);

                $data[$field->id] = $field;
            }
        }

        return $data;
    }
385294
public function fields(array $fields=array(), $exclude=false) {
		$addInc=[];
		if ($fields)
			// collect & set restricted fields for related mappers
			foreach($fields as $i=>$val)
				if(is_int(strpos($val,'.'))) {
					list($key, $relField) = explode('.',$val,2);
					$this->relWhitelist[$key][(int)$exclude][] = $relField;
					unset($fields[$i]);
					$addInc[] = $key;
				}
		$fields = array_unique($fields);
		$schema = $this->whitelist ?: $this->mapper->fields();
		if (!$schema && $this->dbsType != 'sql' && $this->dry()) {
			$this->load();
			$schema = $this->mapper->fields();
			$this->reset();
		}
		// include relation linkage fields to $fields (if $fields is a whitelist)
		if (!$exclude && !empty($fields) && !empty($addInc))
			$fields=array_unique(array_merge($fields,$addInc));
		// include relation linkage fields to existing whitelist (if $fields is a blacklist or there's nothing else to whitelist)
		elseif (!empty($addInc) && $this->whitelist)
			$this->whitelist=array_unique(array_merge($this->whitelist,$addInc));
		// initially merge configured fields into schema (add virtual/rel fields to schema)
		if (!$this->whitelist && $this->fieldConf)
			$schema=array_unique(array_merge($schema,
				array_keys($this->fieldConf),array_keys($this->vFields?:[])));
		// skip if there's nothing to set for own model
		if (!$fields || empty($fields))
			return $schema;
		elseif ($exclude) {
			$this->whitelist=array_diff($schema,$fields);
		} else
			$this->whitelist=$fields;
		$id=$this->dbsType=='sql'?$this->primary:'_id';
		if (!in_array($id,$this->whitelist))
			$this->whitelist[]=$id;
		$this->applyWhitelist();
		return $this->whitelist;
	}
1337664
protected function findOrCreateRolesByName($names)
    {
        if (empty($names)) {
            return [];
        }

        $existing = static::whereIn('name', $names)->get()->keyBy('name');

        return (new Collection($names))
                ->diff($existing->pluck('name'))
                ->map(function ($name) {
                    return static::create(compact('name'));
                })
                ->merge($existing);
    }
361029
private function getAddressFields($_params = array())
    {
        $params = array_merge(
            array(
                'includeHeader' => true,
            ),
            (array) $_params
        );

        $fields = array(
            TextField::create('Address', _t('Addressable.ADDRESS', 'Address')),
            TextField::create('Suburb', _t('Addressable.SUBURB', 'Suburb'))
        );

        if ($params['includeHeader']) {
            array_unshift(
                $fields,
                HeaderField::create('AddressHeader', _t('Addressable.ADDRESSHEADER', 'Address'))
            );
        }


        // Get state field
        $label = _t('Addressable.STATE', 'State');
        $allowedStates = $this->owner->getAllowedStates();
        if (count($allowedStates) >= 1) {
            // If allowed states are restricted, only allow those
            $fields[] = DropdownField::create('State', $label, $allowedStates);
        } elseif (!$allowedStates) {
            // If no allowed states defined, allow the user to type anything
            $fields[] = TextField::create('State', $label);
        }

        // Get postcode field
        $postcode = RegexTextField::create('Postcode', _t('Addressable.POSTCODE', 'Postcode'));
        $postcode->setRegex($this->getPostcodeRegex());
        $fields[] = $postcode;

        // Get country field
        $fields[] = DropdownField::create(
            'Country',
            _t('Addressable.COUNTRY', 'Country'),
            $this->owner->getAllowedCountries()
        );

        $this->owner->extend("updateAddressFields", $fields);

        return $fields;
    }