Skip to content

Instantly share code, notes, and snippets.

@chdemko
Created December 17, 2011 00:50
Show Gist options
  • Save chdemko/1488714 to your computer and use it in GitHub Desktop.
Save chdemko/1488714 to your computer and use it in GitHub Desktop.
/**
* Retrieves field information about the given tables.
*
* @param mixed $table A table name
* @param boolean $typeOnly True to only return field types.
*
* @return array An array of fields.
*
* @since 11.1
* @throws JDatabaseException
*/
public function getTableColumns($table, $typeOnly = true)
{
// Initialise variables.
$result = array();
$table_temp = $this->replacePrefix((string) $table);
// Set the query to get the table fields statement.
$this->setQuery(
'SELECT column_name as Field, data_type as Type, is_nullable as \'Null\', column_default as \'Default\'' .
' FROM information_schema.columns' . ' WHERE table_name = ' . $this->quote($table_temp)
);
$fields = $this->loadObjectList();
// If we only want the type as the value add just that to the list.
if ($typeOnly)
{
foreach ($fields as $field)
{
$result[$field->Field] = preg_replace("/[(0-9)]/", '', $field->Type);
}
}
// If we want the whole field data object add that to the list.
else
{
foreach ($fields as $field)
{
$result[$field->Field] = $field;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment