Zend Framework 2 Db getColumns

What exactly is ‘getColumns’ supposed to return? If I retrieve a table using the abstract interface, it appears the only way I can get the column names is using the Metadata class which my IDE is saying is deprecated. Yet despite hours of searching, I can’t seem to find any information on what is replacing it’s functionality. I thought it might be ‘getColumns’ in the table gateway, but I always get an empty set any time I call it on a defined table.

Answer

RDBMS Metadata documentation

The class ZendDbMetadataMetadata is actually deprecated, but the functionnality is still there. If you look inside the class, you’ll find this note:

/**
 * @deprecated Use ZendDbMetadataSourceFactory::createSourceFromAdapter($adapter)
 */
class Metadata implements MetadataInterface
{
   ...
}

So, instead to instantiate directly the Metadata class, you’ll have to use the factory:

// This is deprecated
// $metadata = new ZendDbMetadataMetadata($adapter);

// This is the correct way to obtain the metadata 
$metadata = ZendDbMetadataSourceFactory::createSourceFromAdapter($adapter);

// get the table names
$tableNames = $metadata->getTableNames();

foreach ($metadata->getTableNames() as $tableName) {
    echo 'In Table ' . $tableName . PHP_EOL;

    $table = $metadata->getTable($tableName);

    echo '    With columns: ' . PHP_EOL;
    foreach ($table->getColumns() as $column) {
        echo '        ' . $column->getName()
        . ' -> ' . $column->getDataType()
        . PHP_EOL;
    }
}