Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge 9da85c1 into 4c68f2c
Browse files Browse the repository at this point in the history
  • Loading branch information
jbelien committed Jul 31, 2019
2 parents 4c68f2c + 9da85c1 commit 4d32b5c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/TableGateway/Feature/MetadataFeature.php
Expand Up @@ -13,6 +13,7 @@
use Zend\Db\TableGateway\Exception;
use Zend\Db\Metadata\Object\TableObject;
use Zend\Db\Metadata\Source\Factory as SourceFactory;
use Zend\Db\Sql\TableIdentifier;

class MetadataFeature extends AbstractFeature
{
Expand Down Expand Up @@ -47,21 +48,31 @@ public function postInitialize()
$t = $this->tableGateway;
$m = $this->metadata;

$tableGatewayTable = is_array($t->table) ? current($t->table) : $t->table;

if ($tableGatewayTable instanceof TableIdentifier) {
$table = $tableGatewayTable->getTable();
$schema = $tableGatewayTable->getSchema();
} else {
$table = $tableGatewayTable;
$schema = null;
}

// get column named
$columns = $m->getColumnNames($t->table);
$columns = $m->getColumnNames($table, $schema);
$t->columns = $columns;

// set locally
$this->sharedData['metadata']['columns'] = $columns;

// process primary key only if table is a table; there are no PK constraints on views
if (! ($m->getTable($t->table) instanceof TableObject)) {
if (! ($m->getTable($table, $schema) instanceof TableObject)) {
return;
}

$pkc = null;

foreach ($m->getConstraints($t->table) as $constraint) {
foreach ($m->getConstraints($table, $schema) as $constraint) {
/** @var $constraint \Zend\Db\Metadata\Object\ConstraintObject */
if ($constraint->getType() == 'PRIMARY KEY') {
$pkc = $constraint;
Expand Down
42 changes: 42 additions & 0 deletions test/integration/Adapter/Driver/Pdo/Mysql/TableGatewayTest.php
Expand Up @@ -3,6 +3,8 @@
namespace ZendIntegrationTest\Db\Adapter\Driver\Pdo\Mysql;

use PHPUnit\Framework\TestCase;
use Zend\Db\Sql\TableIdentifier;
use Zend\Db\TableGateway\Feature\MetadataFeature;
use Zend\Db\TableGateway\TableGateway;

class TableGatewayTest extends TestCase
Expand Down Expand Up @@ -95,4 +97,44 @@ public function testUpdateWithExtendedCharsetFieldName($id)
$this->assertEquals($row->$key, $value);
}
}

public function testTableGatewayStringWithMetadataFeature()
{
$table = 'test';

$tableGateway = new TableGateway($table, $this->adapter, new MetadataFeature());

self::assertInstanceOf(TableGateway::class, $tableGateway);
self::assertSame($table, $tableGateway->getTable());
}

public function testTableGatewayAliasedStringWithMetadataFeature()
{
$table = ['foo' => 'test'];

$tableGateway = new TableGateway($table, $this->adapter, new MetadataFeature());

self::assertInstanceOf(TableGateway::class, $tableGateway);
self::assertSame($table, $tableGateway->getTable());
}

public function testTableGatewayTableIdentifierWithMetadataFeature()
{
$table = new TableIdentifier('test');

$tableGateway = new TableGateway($table, $this->adapter, new MetadataFeature());

self::assertInstanceOf(TableGateway::class, $tableGateway);
self::assertSame($table, $tableGateway->getTable());
}

public function testTableGatewayAliasedTableIdentifierWithMetadataFeature()
{
$table = ['foo' => new TableIdentifier('test')];

$tableGateway = new TableGateway($table, $this->adapter, new MetadataFeature());

self::assertInstanceOf(TableGateway::class, $tableGateway);
self::assertSame($table, $tableGateway->getTable());
}
}

0 comments on commit 4d32b5c

Please sign in to comment.