3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\TableGateway\Feature
;
12 use Zend\Db\Metadata\MetadataInterface
;
13 use Zend\Db\TableGateway\Exception
;
14 use Zend\Db\Metadata\
Object\TableObject
;
15 use Zend\Db\Metadata\Source\Factory
as SourceFactory
;
17 class MetadataFeature
extends AbstractFeature
20 * @var MetadataInterface
22 protected $metadata = null;
27 * @param MetadataInterface $metadata
29 public function __construct(MetadataInterface
$metadata = null)
32 $this->metadata
= $metadata;
34 $this->sharedData
['metadata'] = [
40 public function postInitialize()
42 if ($this->metadata
=== null) {
43 $this->metadata
= SourceFactory
::createSourceFromAdapter($this->tableGateway
->adapter
);
46 // localize variable for brevity
47 $t = $this->tableGateway
;
51 $columns = $m->getColumnNames($t->table
);
52 $t->columns
= $columns;
55 $this->sharedData
['metadata']['columns'] = $columns;
57 // process primary key only if table is a table; there are no PK constraints on views
58 if (! ($m->getTable($t->table
) instanceof TableObject
)) {
64 foreach ($m->getConstraints($t->table
) as $constraint) {
65 /** @var $constraint \Zend\Db\Metadata\Object\ConstraintObject */
66 if ($constraint->getType() == 'PRIMARY KEY') {
73 throw new Exception\
RuntimeException('A primary key for this column could not be found in the metadata.');
76 if (count($pkc->getColumns()) == 1) {
77 $pkck = $pkc->getColumns();
78 $primaryKey = $pkck[0];
80 $primaryKey = $pkc->getColumns();
83 $this->sharedData
['metadata']['primaryKey'] = $primaryKey;