Columns

A column is an instance of weeDbMetaColumn.

Fetching a table's columns list

You can retrieve all the columns using the columns method of your table object. The columns of the table are ordered by their position.

<?php

$aColumns = $oTable->columns();

Additionally, may you require nothing more, you can obtain an array containing only the names of these columns, with the method columnsNames.

<?php

$aColumnsNames = $oTable->columnsNames();

Checking whether a column exists in a table

You can use for this purpose the columnExists method of your table object.

<?php

$bColumnExists = $oTable->columnExists('column_schema');

Fetching a given column from a table

Use the column method of your table object. The returned object is an instance of weeDbMetaColumn.

If the column does not exist in the table, an UnexpectedValueException is thrown.

<?php

try {
	$oColumn = $oTable->column('some_column');
} catch (UnexpectedValueException $e) {
	// The column "some_column" does not exist in the table.
}

Checking whether a column has a default value

To know if a column has a default value, use the method hasDefault on the column object.

<?php

$bHasDefault = $oColumn->hasDefault();

Retrieving the expression of the default value

When the column has a default value, you can retrieve its expression using the method defaultValue. The returned string is an SQL expression that will vary depending on the underlying database.

If the column does not have a default value, an IllegalStateException is thrown.

<?php

try {
	$sDefaultValue = $oColumn->defaultValue();
} catch (IllegalStateException $e) {
	// the column does not have a default value.
}

Checking whether a column is nullable

A column is nullable when its isNullable method returns true.

<?php

$bIsNullable = $oColumn->isNullable();

Retrieving the position of a column in the table

To obtain the position of an arbitrary column, take a look to its num method.

<?php

$iPos = $oColumn->num();

Since columns are ordered by position when they are retrieved using the columns method, you can alternatively retrieve the column position by using the array's index when iterating through it.

<?php

foreach ($oTable->columns() as $iPos => $oColumn) {
	// num() would return the same value as $iPos, so use $iPos
}

Generating a validator for a column

As explained in the validators module documentation, you should always validate the user input before storing it in a database. DbMeta has a basic support of SQL types through the hasValidator and getValidator methods.

<?php

if ($oColumn->hasValidator())
	$oValidator = $oColumn->getValidator();

If the column type is not handled by DbMeta but you call getValidator anyway, an UnhandledTypeException is thrown.