lithium\data\source\database\adapter\MySql::describe()

public method

Gets the column schema for a given MySQL table.

Parameters

  • mixed $entity

    Specifies the table name for which the schema should be returned, or the class name of the model object requesting the schema, in which case the model class will be queried for the correct table name.

  • array $fields

    Any schema data pre-defined by the model.

  • array $meta

Returns

array

Returns an associative array describing the given table's schema, where the array keys are the available fields, and the values are arrays describing each field, containing the following keys:

  • 'type': The field type name

Filter

This method can be filtered.

Source

	public function describe($entity,  $fields = [], array $meta = []) {
		$params = compact('entity', 'meta', 'fields');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			extract($params);

			if ($fields) {
				return $this->_instance('schema', compact('fields'));
			}
			$name = $this->_entityName($entity, ['quoted' => true]);
			$columns = $this->read("DESCRIBE {$name}", ['return' => 'array', 'schema' => [
				'field', 'type', 'null', 'key', 'default', 'extra'
			]]);
			$fields = [];

			foreach ($columns as $column) {
				$schema = $this->_column($column['type']);
				$default = $column['default'];

				if ($default === 'CURRENT_TIMESTAMP') {
					$default = null;
				} elseif ($schema['type'] === 'boolean') {
					$default = !!$default;
				}
				$fields[$column['field']] = $schema + [
					'null'     => ($column['null'] === 'YES' ? true : false),
					'default'  => $default
				];
			}
			return $this->_instance('schema', compact('fields'));
		});
	}