1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Db\TableGateway\Feature;
use Zend\Db\Metadata\MetadataInterface;
use Zend\Db\TableGateway\Exception;
use Zend\Db\Metadata\Object\TableObject;
use Zend\Db\Metadata\Source\Factory as SourceFactory;
class MetadataFeature extends AbstractFeature
{
/**
* @var MetadataInterface
*/
protected $metadata = null;
/**
* Constructor
*
* @param MetadataInterface $metadata
*/
public function __construct(MetadataInterface $metadata = null)
{
if ($metadata) {
$this->metadata = $metadata;
}
$this->sharedData['metadata'] = [
'primaryKey' => null,
'columns' => []
];
}
public function postInitialize()
{
if ($this->metadata === null) {
$this->metadata = SourceFactory::createSourceFromAdapter($this->tableGateway->adapter);
}
// localize variable for brevity
$t = $this->tableGateway;
$m = $this->metadata;
// get column named
$columns = $m->getColumnNames($t->table);
$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)) {
return;
}
$pkc = null;
foreach ($m->getConstraints($t->table) as $constraint) {
/** @var $constraint \Zend\Db\Metadata\Object\ConstraintObject */
if ($constraint->getType() == 'PRIMARY KEY') {
$pkc = $constraint;
break;
}
}
if ($pkc === null) {
throw new Exception\RuntimeException('A primary key for this column could not be found in the metadata.');
}
if (count($pkc->getColumns()) == 1) {
$pkck = $pkc->getColumns();
$primaryKey = $pkck[0];
} else {
$primaryKey = $pkc->getColumns();
}
$this->sharedData['metadata']['primaryKey'] = $primaryKey;
}
}