ZF\Apigility\Admin\Model\DbAutodiscoveryModel::fetchColumns PHP Method

fetchColumns() public method

public fetchColumns ( $module, $version, $adapter_name ) : array
$module
$version
$adapter_name
return array
    public function fetchColumns($module, $version, $adapter_name)
    {
        $tables = [];
        if (!isset($this->config['db']['adapters'])) {
            // error
        }
        $config = $this->config['db']['adapters'];
        $adapter = new Adapter($config[$adapter_name]);
        try {
            $metadata = new Metadata($adapter);
        } catch (InvalidArgumentException $e) {
            if (strpos($e->getMessage(), 'Unknown adapter platform') === false) {
                throw $e;
            }
            return [];
        }
        $tableNames = $metadata->getTableNames(null, true);
        foreach ($tableNames as $tableName) {
            if ($this->moduleHasService($module, $version, $tableName)) {
                continue;
            }
            $tableData = ['table_name' => $tableName];
            $table = $metadata->getTable($tableName);
            $tableData['columns'] = [];
            $constraints = $this->getConstraints($metadata, $tableName);
            /** @var \Zend\Db\Metadata\Object\ColumnObject $column */
            foreach ($table->getColumns() as $column) {
                $item = ['name' => $column->getName(), 'type' => $column->getDataType(), 'required' => !$column->isNullable(), 'filters' => [], 'validators' => [], 'constraints' => []];
                foreach ($constraints as $constraint) {
                    if ($column->getName() == $constraint['column']) {
                        $item['constraints'][] = ucfirst(strtolower($constraint['type']));
                        switch (strtoupper($constraint['type'])) {
                            case 'PRIMARY KEY':
                                break;
                            case 'FOREIGN KEY':
                                $constraintObj = $this->getConstraintForColumn($metadata, $tableName, $column->getName());
                                $validator = $this->validators['foreign_key'];
                                $referencedColumns = $constraintObj->getReferencedColumns();
                                $validator['options'] = ['adapter' => $adapter_name, 'table' => $constraintObj->getReferencedTableName(), 'field' => $referencedColumns[0]];
                                $item['validators'][] = $validator;
                                break;
                            case 'UNIQUE':
                                $validator = $this->validators['unique'];
                                $validator['options'] = ['adapter' => $adapter_name, 'table' => $tableName, 'field' => $column->getName()];
                                $item['validators'][] = $validator;
                                break;
                        }
                    }
                }
                if (in_array(strtolower($column->getDataType()), ['varchar', 'text'])) {
                    $item['length'] = $column->getCharacterMaximumLength();
                    if (in_array('Primary key', array_values($item['constraints']))) {
                        unset($item['filters']);
                        unset($item['validators']);
                        $tableData['columns'][] = $item;
                        continue;
                    }
                    $item['filters'] = $this->filters['text'];
                    $validator = $this->validators['text'];
                    $validator['options']['max'] = $column->getCharacterMaximumLength();
                    $item['validators'][] = $validator;
                } elseif (in_array(strtolower($column->getDataType()), ['tinyint', 'smallint', 'mediumint', 'int', 'bigint'])) {
                    $item['length'] = $column->getNumericPrecision();
                    if (in_array('Primary key', array_values($item['constraints']))) {
                        unset($item['filters']);
                        unset($item['validators']);
                        $tableData['columns'][] = $item;
                        continue;
                    }
                    $item['filters'] = $this->filters['integer'];
                }
                $tableData['columns'][] = $item;
            }
            $tables[] = $tableData;
        }
        return $tables;
    }

Usage Example

 public function discoverAction()
 {
     $module = $this->params()->fromRoute('name', false);
     $version = $this->params()->fromRoute('version', false);
     $adapter_name = urldecode($this->params()->fromRoute('adapter_name', false));
     $data = $this->model->fetchColumns($module, $version, $adapter_name);
     return new ViewModel(['payload' => $data]);
 }