Primary keys

Checking whether a table has a primary key

To check whether a table has a primary key, use the hasPrimaryKey method of your table object.

<?php

$bHasPrimaryKey = $oTable->hasPrimaryKey();

Fetching the primary key of a table

When you need to retrieve the primary key of a table, you can use the primaryKey method of your table object. The returned object is an instance of weeDbMetaPrimaryKey.

If the table does not have a primary key, an IllegalStateException is thrown.

<?php

try {
	$oPrimaryKey = $oTable->primaryKey();
} catch (IllegalStateException $e) {
	// The table does not have a primary key.
}

Fetching the names of the columns of a primary key

An instance of weeDbMetaPrimaryKey is not so useful by itself. The real bit of important data is the names of the columns of the primary key. These names are returned by the primary key's object columnsNames method.

<?php

$aColumnsNames = $oPrimaryKey->columnsNames();