예제 #1
0
 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;
 }
예제 #2
0
 /**
  * Constructor
  *
  * @param Adapter $adapter
  */
 public function __construct(Adapter $adapter)
 {
     $this->source = Source\Factory::createSourceFromAdapter($adapter);
 }
예제 #3
0
 protected function setSelectOrder(Select $selectSQL, Query $query)
 {
     $sort = $query->getSort();
     $sortFields = !$sort ? [$this->dbTable->table . '.' . $this->getIdentifier() => SortNode::SORT_ASC] : $sort->getFields();
     /** @var Adapter $adapter */
     $adapter = $this->dbTable->getAdapter();
     $metadata = Factory::createSourceFromAdapter($adapter);
     foreach ($sortFields as $ordKey => $ordVal) {
         if (!preg_match('/[\\w]+\\.[\\w]+/', $ordKey)) {
             $fined = false;
             /** @var DbTable $entity */
             foreach ($this->joinedEntities as $entity) {
                 if (is_object($entity)) {
                     $entityTable = $metadata->getTable($entity->dbTable->table);
                     /** @var Column $column */
                     foreach ($entityTable->getColumns() as $column) {
                         if ($column->getName() == $ordKey) {
                             $ordKey = $entity->dbTable->table . '.' . $ordKey;
                             $fined = true;
                             break;
                         }
                     }
                 }
                 if ($fined) {
                     break;
                 }
             }
             if (!$fined) {
                 $ordKey = $this->dbTable->table . '.' . $ordKey;
             }
         }
         if ((int) $ordVal === SortNode::SORT_DESC) {
             $selectSQL->order($ordKey . ' ' . Select::ORDER_DESCENDING);
         } else {
             $selectSQL->order($ordKey . ' ' . Select::ORDER_ASCENDING);
         }
     }
     return $selectSQL;
 }
예제 #4
0
 /**
  * Checks if the table exists
  *
  * @param string $tableName
  * @return bool
  */
 public function hasTable($tableName)
 {
     $dbMetadata = Source\Factory::createSourceFromAdapter($this->db);
     $tableNames = $dbMetadata->getTableNames();
     return in_array($tableName, $tableNames);
 }
예제 #5
0
 /**
  * initialize bound table
  */
 protected function initBound()
 {
     if (!isset($this->boundTables) || empty($this->boundTables)) {
         /** @var Adapter $adapter */
         $adapter = $this->dbTable->getAdapter();
         $tableManager = new TableManagerMysql($adapter);
         $metadata = Factory::createSourceFromAdapter($adapter);
         $this->boundTables = ['single' => [], 'multiple' => []];
         /** @var $constraint \Zend\Db\Metadata\Object\ConstraintObject */
         foreach ($metadata->getConstraints($this->dbTable->table) as $constraint) {
             if ($constraint->isForeignKey()) {
                 $this->boundTables['single'][$constraint->getReferencedTableName()] = ['table' => new Composite(new TableGateway($constraint->getReferencedTableName(), $adapter)), 'myColumn' => $constraint->getColumns()[0], 'column' => $constraint->getReferencedColumns()[0]];
             }
         }
         foreach ($tableManager->getLinkedTables($this->dbTable->table) as $linkedTable) {
             $this->boundTables['multiple'][$linkedTable['TABLE_NAME']] = ['table' => new Composite(new TableGateway($linkedTable['TABLE_NAME'], $adapter)), 'column' => $linkedTable['COLUMN_NAME']];
         }
     }
 }