Gets the primary key name from a given fields collection

@param array $fields
@param string $default

@return null | string
559703
protected function getHeaderFieldAccessor(array $fields, $modelName)
    {
        $field = $this->getHeaderField($fields);

        return !is_null($field) ? sprintf('$%s->%s', $this->getSingularVariable($modelName), $field->name) : '$title';
    }
1171230
protected function getFieldNameFromData($fieldName, $data, $default = null)
    {
        /*
         * Array field name, eg: field[key][key2][key3]
         */
        $keyParts = HtmlHelper::nameToArray($fieldName);
        $lastField = end($keyParts);
        $result = $data;

        /*
         * Loop the field key parts and build a value.
         * To support relations only the last field should return the
         * relation value, all others will look up the relation object as normal.
         */
        foreach ($keyParts as $key) {

            if ($result instanceof Model && $result->hasRelation($key)) {
                if ($key == $lastField) {
                    $result = $result->getRelationValue($key) ?: $default;
                }
                else {
                    $result = $result->{$key};
                }
            }
            elseif (is_array($result)) {
                if (!array_key_exists($key, $result)) {
                    return $default;
                }
                $result = $result[$key];
            }
            else {
                if (!isset($result->{$key})) {
                    return $default;
                }
                $result = $result->{$key};
            }

        }

        return $result;
    }
1057696
protected function getFieldsToDisplay(array $fields = null)
    {
        if (!empty($fields)) {
            return array_filter($this->fields, function ($field) use ($fields) {
                return in_array($field->name, $fields);
            });
        }

        return $this->fields;
    }
35460
public static function get($array, $key, $default = null)
    {
        $key = static::normalizeKey($key);

        if ($key === null || $key === '')
        {
            return $array;
        }

        $keys = explode('.', $key);
        $currentElement = $array;

        foreach ($keys as $currentKey)
        {
            if (!is_array($currentElement) || !array_key_exists($currentKey, $currentElement))
            {
                return $default;
            }

            $currentElement = $currentElement[(string) $currentKey];
        }

        return $currentElement;
    }
879302
public function aliasFields($fields, $defaultAlias = null)
    {
        $aliased = [];
        foreach ($fields as $alias => $field) {
            if (is_numeric($alias) && is_string($field)) {
                $aliased += $this->aliasField($field, $defaultAlias);
                continue;
            }
            $aliased[$alias] = $field;
        }

        return $aliased;
    }
822370
public function iterator(array $fields = null)
    {
        if ($this->qiterator) {
            return $this->qiterator;
        }
        $aliases = [];
        $getAlias = function ($name) use (&$aliases) {
            // to bypass use: return $name;
            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
        };
        $table = $this->definition->getName();
        if ($fields !== null) {
            $this->columns($fields);
        }
        $relations = $this->withr;
        foreach ($relations as $k => $v) {
            $getAlias($k);
        }

        $f = $this->fields;
        $w = $this->where;
        $h = $this->having;
        $o = $this->order;
        $g = $this->group;
        $j = array_map(function ($v) { return clone $v; }, $this->joins);

        $porder = [];
        foreach ($this->definition->getPrimaryKey() as $field) {
            $porder[] = $this->getColumn($field)['name'];
        }

        foreach ($this->definition->getRelations() as $k => $relation) {
            foreach ($f as $kk => $field) {
                if (strpos($field, $k . '.') === 0) {
                    $relations[$k] = [ $relation, $table ];
                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
                }
            }
            foreach ($w as $kk => $v) {
                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
                    $relations[$k] = [ $relation, $table ];
                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
                }
            }
            foreach ($h as $kk => $v) {
                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
                    $relations[$k] = [ $relation, $table ];
                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
                }
            }
            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
                $relations[$k] = [ $relation, $table ];
                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
            }
            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
                $relations[$k] = [ $relation, $table ];
                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
            }
            foreach ($j as $kk => $v) {
                foreach ($v->keymap as $kkk => $vv) {
                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
                        $relations[$k] = [ $relation, $table ];
                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
                    }
                }
            }
        }
        $select = [];
        foreach ($f as $k => $field) {
            $select[] = $field . (!is_numeric($k) ? ' ' . $k : '');
        }
        foreach ($this->withr as $name => $relation) {
            foreach ($relation[0]->table->getColumns() as $column) {
                $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column);
            }
        }
        $sql = 'SELECT '.implode(', ', $select).' FROM '.$table.' ';
        $par = [];
        $many = false;
        foreach ($j as $k => $v) {
            if ($v->many) {
                $many = true;
            }
            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
            $tmp = [];
            foreach ($v->keymap as $kk => $vv) {
                $tmp[] = $kk.' = '.$vv;
            }
            $sql .= implode(' AND ', $tmp) . ' ';
        }
        foreach ($relations as $relation => $v) {
            $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
            $v = $v[0];
            if ($v->many || $v->pivot) {
                $many = true;
            }
            if ($v->pivot) {
                $alias = $getAlias($relation.'_pivot');
                $sql .= 'LEFT JOIN '.$v->pivot->getName().' '.$alias.' ON ';
                $tmp = [];
                foreach ($v->keymap as $kk => $vv) {
                    $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
                }
                $sql .= implode(' AND ', $tmp) . ' ';
                $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($relation).' ON ';
                $tmp = [];
                foreach ($v->pivot_keymap as $kk => $vv) {
                    $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' ';
                }
                $sql .= implode(' AND ', $tmp) . ' ';
            } else {
                $alias = $getAlias($relation);

                $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
                $tmp = [];
                foreach ($v->keymap as $kk => $vv) {
                    $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
                }
                if ($v->sql) {
                    $tmp[] = $v->sql . ' ';
                    $par = array_merge($par, $v->par ?? []);
                }
                $sql .= implode(' AND ', $tmp) . ' ';
            }
        }

        if ($many && count($porder) && $this->li_of[2] === 1) {
            $ids = $this->ids();
            if (count($ids)) {
                if (count($porder) > 1) {
                    $pkw = [];
                    foreach ($porder as $name) {
                        $pkw[] = $name . ' = ?';
                    }
                    $pkw = '(' . implode(' AND ', $pkw) . ')';
                    $pkp = [];
                    foreach ($ids as $id) {
                        foreach ($id as $p) {
                            $pkp[] = $p;
                        }
                    }
                    $w[] = [
                        implode(' OR ', array_fill(0, count($ids), $pkw)),
                        $pkp
                    ];
                } else {
                    $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ];
                }
            } else {
                $w[] = [ '1=0', [] ];
            }
        }
        if (count($w)) {
            $sql .= 'WHERE ';
            $tmp = [];
            foreach ($w as $v) {
                $tmp[] = '(' . $v[0] . ')';
                $par = array_merge($par, $v[1]);
            }
            $sql .= implode(' AND ', $tmp).' ';
        }
        if (count($g)) {
            $sql .= 'GROUP BY ' . $g[0] . ' ';
            $par = array_merge($par, $g[1]);
        }
        if (count($h)) {
            $sql .= 'HAVING ';
            $tmp = [];
            foreach ($h as $v) {
                $tmp[] = '(' . $v[0] . ')';
                $par = array_merge($par, $v[1]);
            }
            $sql .= implode(' AND ', $tmp).' ';
        }
        if (count($o)) {
            $sql .= 'ORDER BY ' . $o[0] . ' ';
            $par = array_merge($par, $o[1]);
        }
        if (count($porder)) {
            $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC';
            $porder = array_map(function ($v) use ($pdir) { return $v . ' ' . $pdir; }, $porder);
            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
        }
        if ((!$many || $this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) {
            if ($this->db->driverName() === 'oracle') {
                if ((int)$this->db->driverOption('version', 0) >= 12) {
                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
                } else {
                    $f = array_map(function ($v) {
                        $v = explode(' ', trim($v), 2);
                        if (count($v) === 2) { return $v[1]; }
                        $v = explode('.', $v[0], 2);
                        return count($v) === 2 ? $v[1] : $v[0];
                    }, $select);
                    $sql = "SELECT " . implode(', ', $f) . " 
                            FROM (
                                SELECT tbl__.*, rownum rnum__ FROM (
                                    " . $sql . "
                                ) tbl__ 
                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
                            ) WHERE rnum__ > " . $this->li_of[1];
                }
            } else {
                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
            }
        }
        return $this->qiterator = new TableQueryIterator(
            $this->db->get($sql, $par, null, false, false), 
            $this->pkey,
            $this->withr,
            $aliases
        );
    }
378656
public function value($field, $default = null)
    {
        $result = null;
        if (!$result) {
            if (isset($this->options['field'])) {
                unset($this->options['field']);
            }
            $cursor = $this->field($field)->fetchCursor(true)->find();
            $cursor->setTypeMap(['root' => 'array']);
            $resultSet = $cursor->toArray();
            $data      = isset($resultSet[0]) ? $resultSet[0] : null;
            $result    = $data[$field];
        } else {
            // 清空查询条件
            $this->options = [];
        }
        return !is_null($result) ? $result : $default;
    }
1214586
public static function getValue($array, $key, $default = null)
    {
        if (is_string($key)) {
            // Iterate path
            $keys = explode('.', $key);
            foreach ($keys as $key) {
                if (!isset($array[$key])) {
                    return $default;
                }
                $array = &$array[$key];
            }
            // Get value
            return $array;
        } elseif (is_null($key)) {
            // Get all data
            return $array;
        }
        return null;
    }
973219
private function getIn(array $array, array $keys, $default = null)
    {
        if (!$keys) {
            return $array;
        }

        // This is a micro-optimization, it is fast for non-nested keys, but fails for null values
        if (\count($keys) === 1 && isset($array[$keys[0]])) {
            return $array[$keys[0]];
        }

        $current = $array;
        foreach ($keys as $key) {
            if (!\is_array($current) || !\array_key_exists($key, $current)) {
                return $default;
            }

            $current = $current[$key];
        }

        return $current;
    }
77229
protected function getNewPrimaryKey(Field $primaryKey = null, $defaultName = 'id')
    {
        $stub = $this->getStubContent('model-primary-key');

        $this->replacePrimaryKey($stub, $this->getPrimaryKeyName($primaryKey, $defaultName));

        if (!is_null($primaryKey) && !$primaryKey->isNumeric()) {
            $lines = explode("\n", $stub);
            $last = end($lines);
            $spaces = str_repeat(' ', $this->getIndent($last, 'p'));
            $stub .= PHP_EOL . $spaces . 'protected $keyType = \'string\';' . PHP_EOL;
            $stub .= $spaces . 'public $incrementing = false;' . PHP_EOL;
        }

        return $stub;
    }
296045
public function get($key, $default = null)
    {
        $value = parent::get($key, $default);
        if (is_array($value)) {
            $value = array_shift($value);
        }
        return $value;
    }
480570
public function getArray($key, array $default = array())
    {
        $value = $this->get($key, null);

        return null !== $value ? (array) $value : (array) $default;
    }
936126
function autovalue($fieldname, $default = null) {
		if ($this->autocomplete !== null) {
			$fieldname = rtrim($fieldname, '[]');
			if (isset($this->autocomplete[$fieldname])) {
				return $this->autocomplete[$fieldname];
			}
		}

		// no auto complete value found
		return $default;
	}
708647
protected static function restGetSortFields($default = [])
    {
        $sort = Request::getInstance()->query('_sort', false);
        if (!$sort) {
            return $default;
        }

        $sorters = [];
        $fields = explode(',', $sort);
        foreach ($fields as $sort) {
            $sortField = $sort;
            $sortDirection = 1;

            $sortDirectionSign = substr($sort, 0, 1);
            if ($sortDirectionSign == '+' || $sortDirectionSign == '-') {
                $sortField = substr($sort, 1);
                $sortDirection = $sortDirectionSign == '+' ? 1 : -1;
            }

            $sorters[$sortField] = $sortDirection;
        }

        return $sorters;
    }
547678
public function get($key, $default = null)
    {
        return array_key_exists($key, $this->collection) ? $this->collection[$key] : $default;
    }
1126780
public function get($reference, $default = null)
    {
        $reference = $this->computeKeyFQN($reference);

        // first look for exact match
        if (isset(self::$data[$reference])) {
            return self::$data[$reference];
        }

        // otherwise use a matcher to return a
        // collection of matching entries
        $matcher = $this->getMatcher();
        $matches = new Collection();

        Collection::cast(self::$data)->each(function (&$value, $key) use ($matcher, $reference, $matches) {
            if ($matcher->match($reference, $key)) {
                $matches[$key] = $value;
            }
        });

        if (!$matches->isEmpty()) return $matches;

        return $default;
    }
237119
public function getField($config, $field, $default = '')
    {
        $data = (isset($config['config'][$field])) ? $config['config'][$field] : $default;

        return $data;
    }