@param \Reliese\Coders\Model\Model $model

@todo: Delegate workload to SchemaManager and ModelManager

@return array
350131
public function referencing(Model $model)
    {
        $references = [];

        // TODO: SchemaManager should do this
        foreach ($this->schemas as $schema) {
            $references = array_merge($references, $schema->referencing($model->getBlueprint()));
        }

        // TODO: ModelManager should do this
        foreach ($references as &$related) {
            $blueprint = $related['blueprint'];
            $related['model'] = $model->getBlueprint()->is($blueprint->schema(), $blueprint->table())
                ? $model
                : $this->makeModel($blueprint->schema(), $blueprint->table(), false);
        }

        return $references;
    }
664757
protected function prepareModel(Model $model)
    {
        if (is_callable($this->prepareModel)) {
            call_user_func($this->prepareModel, $model);
        } else {
            foreach ($model->attributes() as $attribute) {
                $value = Yii::$app->settings->get($this->getSection($model), $attribute);

                if (!is_null($value)) {
                    $model->{$attribute} = $value;
                }
            }
        }
    }
848929
protected function getModelPermissions($model)
    {
        $tree = $this->treeBuilder->getTree();
        $psrClass = '';
        if (is_string($model)) {
            $psrClass = $model;
        } elseif (is_object($model)) {
            $psrClass = get_class($model);
        }

        if (!empty($tree['models']) && array_key_exists($psrClass, $tree['models'])) {
            return $tree['models'][$psrClass];
        }

        return [];
    }
1090328
protected function getAttributes($model)
    {
        $attributes = $model->attributesToArray();
        foreach( $this->immutableAttributes as $attribute ) {
            unset( $attributes[ $attribute] );
        }

        return $attributes;
    }
513057
public function sources($model = null) {
		$params = compact('model');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$schema = $this->connection->quote($this->_config['schema']);

			$sql  = "SELECT table_name as name FROM INFORMATION_SCHEMA.tables";
			$sql .= " WHERE table_schema = {$schema}";

			if (!$result = $this->_execute($sql)) {
				return null;
			}
			$sources = [];

			foreach ($result as $row) {
				$sources[] = $row[0];
			}
			return $sources;
		});
	}
1261263
protected function getPolicyColumn(Model $model)
    {

        $connection = $model->getConnection();
        $table      = $connection->getTablePrefix() . $model->getTable();
        $schema     = $connection->getDoctrineSchemaManager($table);
        $columns    = Cache::remember('keys_' . $table, 30, function() use($model) {
                    return Schema::getColumnListing($model->getTable());
                });


        $column = null;
        foreach (self::$patternedColumns as $name) {
            if (!is_null($column)) {
                break;
            }
            $column = array_search($name, $columns) !== false ? $name : null;
        }
        if (is_null($column)) {
            foreach ($schema->listTableForeignKeys($table) as $foreignKeyConstraint) {
                if ($foreignKeyConstraint->getForeignTableName() !== 'tbl_users') {
                    continue;
                }
                $column = current($foreignKeyConstraint->getLocalColumns());
            }
        }

        return $column;
    }
1133400
public function getModelCallbacks(Model $model, $scope = '*') {
        $callbacks = array();
        $models = Configure::read('Admin.modelCallbacks');

        if (isset($models[$model->qualifiedName])) {
            foreach ($models[$model->qualifiedName] as $method => $options) {
                if (is_string($options)) {
                    $options = array('title' => $options, 'access' => 'update', 'scope' => '*');
                } else {
                    $options = $options += array('access' => 'update', 'scope' => '*');
                }

                if ($options['scope'] !== $scope) {
                    continue;
                }

                if ($this->hasAccess($model->qualifiedName, $options['access'])) {
                    $callbacks[$method] = __d('admin', $options['title'], $model->singularName);
                }
            }
        }

        return $callbacks;
    }
659687
protected static function decryptAttributes(Model $model)
    {
        foreach (static::getEncryptedAttributes() as $attribute) {
            try {
                if (is_string($model->$attribute)) {
                    $model->$attribute = static::getEncrypter()->decrypt($model->$attribute);
                }
            } catch (DecryptException $e) {
            }
        }

        return $model;
    }
1352193
protected function getBlameableFields(Model $model)
    {
        if (isset($this->fields)) {
            return $this->fields;
        }

        $this->fields = self::findBlameableFields($model);

        return $this->fields;
    }
488963
public static function castModel(Model $model)
    {
        $attributes = array_merge(
            $model->getAttributes(),
            $model->getRelations()
        );

        $visible = array_flip(
            $model->getVisible() ?: array_diff(array_keys($attributes), $model->getHidden())
        );

        $results = [];

        foreach (array_intersect_key($attributes, $visible) as $key => $value) {
            $results[(isset($visible[$key]) ? Caster::PREFIX_VIRTUAL : Caster::PREFIX_PROTECTED).$key] = $value;
        }

        return $results;
    }
449194
public function scaffoldViewColumns(Model $model)
    {
        $items = new Mutable();

        $attributes = \admin\helpers\eloquent_attributes($model);

        foreach ($attributes as $key => $value) {
            $items->push($key);
        }

        return $items->build(
            new GridDecorator($model)
        );
    }
902043
protected function collectColumns(Model $model = null)
    {
        if (!$model) {
            return new MutableCollection([]);
        }

        $pk = $model->getKeyName();

        $fillable = array_merge(
            is_array($pk) ? $pk : [$pk],
            $model->getFillable()
        );
        $hidden = $model->getHidden();

        if ($model instanceof Translatable && method_exists($model, 'getTranslatedAttributes')) {
            $fillable = array_merge($fillable, $model->getTranslatedAttributes());
            $hidden = array_merge($hidden, $model->getTranslationModel()->getHidden());
        }

        $fillable = array_unique(array_diff($fillable, $hidden));

        /**
         * Create collection.
         */
        $elements = new MutableCollection($fillable);

        if ($this->includeDateColumns && count($dates = $model->getDates())) {
            // allow setting specific timestamp: created_at
            if (is_string($this->includeDateColumns)) {
                $dates = array_intersect($dates, [$this->includeDateColumns]);

                $elements = $elements->merge($dates);
            } else {
                // add timestamps group
                $elements->group('dates', function (Group $group) use ($dates) {
                    $group->merge($dates);

                    return $group;
                });
            }
        }

        return $elements->build(
            new GridDecorator($model)
        );
    }
410604
protected function _getSchema($model = null) {
		$result = null;
		if (empty($model)) {
			return $result;
		}

		$modelObj = ClassRegistry::init($model, true);
		if ($modelObj === false) {
			return $result;
		}

		$result = $modelObj->schema();
		if (empty($modelObj->virtualFields)) {
			return $result;
		}

		foreach ($modelObj->virtualFields as $virtualFieldName => $virtualFieldVal) {
			$result[$virtualFieldName] = ['type' => 'string'];
		}

		return $result;
	}
837414
public function fieldsOf($model)
    {
        $method = 'fields_get';

        $result = $this->call($model, $method, []);


        return $this->makeResponse($result);
    }
187236
protected function runSerializeTransformsOnModel(Model $model)
    {
        // Loop through all the transform functions
        foreach ($this->serialize_transforms as $transform) {

            // Call the transform and if nothing is retuned, return nothing. This will
            // be used by runSerializeTransformsOnCollection() to filter out models.
            if (!$model = call_user_func($transform, $model)) {
                return;
            }
        }

        // Return the transformed model
        return $model;
    }
769457
protected function _getAliases(Model $Model) {
		$aliases = array_merge(array(
			//$Model->name,
			$Model->alias
				), $this->config[$Model->alias]['aliases']
		);

		foreach ($Model->associations() as $associationType) {
			foreach ($Model->$associationType as $associationKey => $association) {
				if ($association['className'] === $Model->name) {
					$aliases[] = $associationKey;
				}
			}
		}

		return $aliases;
	}
351080
protected function showRelatoins(Model $model)
    {
        $name = $this->manager->getClassName($model);

        $this->title("Relations of Model {$name}: ");

        $relations = $this->manager->getRelations($model);
        $tbody = [];

        foreach ($relations as $relation) {
            $relationName = $this->manager->getClassName($relation);
            $related = $relation->getRelated() ?:null;

            $tbody[] = [
                $this->paintString($relationName, 'brown'),
                $related ? $this->manager->getClassName($related) : '',
            ];
        }

        $this->table(['relation', 'model'], $tbody);
    }