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

Commit

Permalink
Merge branch 'hotfix/392'
Browse files Browse the repository at this point in the history
Close #392
Fix #391
  • Loading branch information
michalbundyra committed Dec 31, 2019
2 parents f76db34 + 8879204 commit 1f83419
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,8 @@ All notable changes to this project will be documented in this file, in reverse

- [#395](https://github.com/zendframework/zend-db/pull/395) fixes PHP 7.4 compatibility.

- [#392](https://github.com/zendframework/zend-db/pull/392) fixes `MetadataFeature` to work with `TableIdentifier`.

## 2.10.0 - 2019-02-25

### Added
Expand Down
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
24 changes: 24 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,26 @@ public function testUpdateWithExtendedCharsetFieldName($id)
$this->assertEquals($row->$key, $value);
}
}

/**
* @dataProvider tableProvider
* @param string|TableIdentifier|array $table
*/
public function testTableGatewayWithMetadataFeature($table)
{
$tableGateway = new TableGateway($table, $this->adapter, new MetadataFeature());

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

public function tableProvider()
{
return [
'string' => ['test'],
'aliased string' => [['foo' => 'test']],
'TableIdentifier' => [new TableIdentifier('test')],
'aliased TableIdentifier' => [['foo' => new TableIdentifier('test')]],
];
}
}

0 comments on commit 1f83419

Please sign in to comment.