/** * Função que retorna com os dados de preenchimento do formulário * @return array */ public function populateForm($id) { $db = \Cityware\Db\Factory::factory('zend'); $platform = $db->getAdapter()->getPlatform(); /* Verifica o schema se está setado ou não */ $schema = (isset($this->configForm['schema']) and !empty($this->configForm['schema'])) ? $this->configForm['schema'] : null; /* Define a tabela */ $db->from($this->configForm['table'], 'tb', $schema); /* Define os campos */ //$db->select('*'); /* Define a coluna primária e monta a condição do SELECT */ $metadata = new \Zend\Db\Metadata\Metadata($db->getAdapter()); $constraints = $metadata->getTable($this->configForm['table'], $schema)->getConstraints(); $varColunaPk = null; foreach ($constraints as $value) { if ($value->getType() == 'PRIMARY KEY') { $varColunaPk = $value->getColumns(); } } $db->where($platform->quoteIdentifier('tb') . "." . $platform->quoteIdentifier(reset($varColunaPk)) . ' = ' . $id); /* Executa o SELECT */ $db->setDebug(false); $rs = $db->executeSelectQuery(); /* Formata qualquer coluta do tipo data */ $return = \Cityware\Format\Date::formatDate($rs, 'd/m/Y'); /* Retorna um array com os dados de preenchimento do formulário */ return $return[0]; }
/** */ public function getTableColumns($tableName) { $sm = $this->getServiceLocator(); $adapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $metadata = new \Zend\Db\Metadata\Metadata($this->adapter); $table = $metadata->getTable($tableName); return $table->getColumns(); }
/** * Função de criação dos arquivos de Formulário * @throws \Exception */ private function createFormFiles() { $db = \Cityware\Db\Factory::factory('zend'); $tableMetadata = new \Zend\Db\Metadata\Metadata($db->getAdapter()); $tableInfo = $tableMetadata->getTable($this->getTable(), $this->getSchema()); $iniFile = $this->genConfigFormIni(); $iniFile .= $this->genFieldsFormIni($tableInfo); $iniFile .= $this->genButtonsFormIni(); $translateFile = $this->genTranslateFormArray($tableInfo); $moduleName = ucfirst($this->getModule()); $moduleNameLower = strtolower($this->getModule()); try { if (!empty($this->formName)) { file_put_contents($this->dstIniDirectory . $this->formName . '.ini', $iniFile); chmod($this->dstIniDirectory . $this->formName . '.ini', 0777); file_put_contents($this->dstTranslateDirectory . $this->formName . '.php', $translateFile); chmod($this->dstTranslateDirectory . $this->formName . '.php', 0777); $template_Form = file_get_contents($this->srcTemplateDirectory . 'Template_Form.tmpl'); $templateForm = str_replace("%moduleName%", $moduleName, str_replace("%moduleNameLower%", $moduleNameLower, $template_Form)); file_put_contents($this->dstViewDirectory . $this->formName . '.phtml', $templateForm); chmod($this->dstViewDirectory . $this->formName . '.phtml', 0644); } else { file_put_contents($this->dstIniDirectory . 'add.ini', $iniFile); chmod($this->dstIniDirectory . 'add.ini', 0777); file_put_contents($this->dstTranslateDirectory . 'add.php', $translateFile); chmod($this->dstTranslateDirectory . 'add.php', 0777); file_put_contents($this->dstIniDirectory . 'edit.ini', $iniFile); chmod($this->dstIniDirectory . 'edit.ini', 0777); file_put_contents($this->dstTranslateDirectory . 'edit.php', $translateFile); chmod($this->dstTranslateDirectory . 'edit.php', 0777); $template_Form = file_get_contents($this->srcTemplateDirectory . 'Template_Form.tmpl'); $templateForm = str_replace("%moduleName%", $moduleName, str_replace("%moduleNameLower%", $moduleNameLower, $template_Form)); file_put_contents($this->dstViewDirectory . 'add.phtml', $templateForm); chmod($this->dstViewDirectory . 'add.phtml', 0644); file_put_contents($this->dstViewDirectory . 'edit.phtml', $templateForm); chmod($this->dstViewDirectory . 'edit.phtml', 0644); } } catch (Exception $exc) { throw new Exception('Não foi possivel criar o arquivo de configuração do formulário! <br />' . $exc->getMessage(), 500); } }
/** * Função de criação dos arquivos do Datagrid * @throws \Exception */ private function createDatagridFiles() { $db = \Cityware\Db\Factory::factory('zend'); $tableMetadata = new \Zend\Db\Metadata\Metadata($db->getAdapter()); $tableInfo = $tableMetadata->getTable($this->getTable(), $this->getSchema()); $iniFile = $this->genConfigDatagridIni(); $iniFile .= $this->genFieldsDatagridIni($tableInfo); $translateFile = $this->genTranslateDatagridArray($tableInfo); $moduleName = ucfirst($this->getModule()); $moduleNameLower = strtolower($this->getModule()); try { if (!empty($this->datagridName)) { file_put_contents(MODULES_PATH . ucfirst($this->getModule()) . DS . 'src' . DS . ucfirst($this->getModule()) . DS . 'ini' . DS . strtolower($this->getController()) . DS . $this->datagridName . '.ini', $iniFile); chmod(MODULES_PATH . ucfirst($this->getModule()) . DS . 'src' . DS . ucfirst($this->getModule()) . DS . 'ini' . DS . strtolower($this->getController()) . DS . $this->datagridName . '.ini', 0777); file_put_contents(MODULES_PATH . ucfirst($this->getModule()) . DS . 'src' . DS . ucfirst($this->getModule()) . DS . 'translate' . DS . 'pt_BR' . DS . strtolower($this->getController()) . DS . $this->datagridName . '.php', $translateFile); chmod(MODULES_PATH . ucfirst($this->getModule()) . DS . 'src' . DS . ucfirst($this->getModule()) . DS . 'translate' . DS . 'pt_BR' . DS . strtolower($this->getController()) . DS . $this->datagridName . '.php', 0777); $datagrid_template_phtml = file_get_contents(dirname(__FILE__) . DS . 'Datagrid' . DS . 'Template_Datagrid_Index.tpl'); $datagridTemplatePhtml = str_replace("%moduleName%", $moduleName, str_replace("%moduleNameLower%", $moduleNameLower, $datagrid_template_phtml)); file_put_contents(MODULES_PATH . ucfirst($this->getModule()) . DS . 'view' . DS . strtolower($this->getModule()) . DS . strtolower($this->getController()) . DS . $this->datagridName . '.phtml', $datagridTemplatePhtml); chmod(MODULES_PATH . ucfirst($this->getModule()) . DS . 'view' . DS . strtolower($this->getModule()) . DS . strtolower($this->getController()) . DS . $this->datagridName . '.phtml', 0644); } else { file_put_contents(MODULES_PATH . ucfirst($this->getModule()) . DS . 'src' . DS . ucfirst($this->getModule()) . DS . 'ini' . DS . strtolower($this->getController()) . DS . 'datagrid.ini', $iniFile); chmod(MODULES_PATH . ucfirst($this->getModule()) . DS . 'src' . DS . ucfirst($this->getModule()) . DS . 'ini' . DS . strtolower($this->getController()) . DS . 'datagrid.ini', 0777); file_put_contents(MODULES_PATH . ucfirst($this->getModule()) . DS . 'src' . DS . ucfirst($this->getModule()) . DS . 'translate' . DS . 'pt_BR' . DS . strtolower($this->getController()) . DS . 'datagrid.php', $translateFile); chmod(MODULES_PATH . ucfirst($this->getModule()) . DS . 'src' . DS . ucfirst($this->getModule()) . DS . 'translate' . DS . 'pt_BR' . DS . strtolower($this->getController()) . DS . 'datagrid.php', 0777); $datagrid_template_phtml = file_get_contents(dirname(__FILE__) . DS . 'Datagrid' . DS . 'Template_Datagrid_Index.tpl'); $datagridTemplatePhtml = str_replace("%moduleName%", $moduleName, str_replace("%moduleNameLower%", $moduleNameLower, $datagrid_template_phtml)); file_put_contents(MODULES_PATH . ucfirst($this->getModule()) . DS . 'view' . DS . strtolower($this->getModule()) . DS . strtolower($this->getController()) . DS . 'index.phtml', $datagridTemplatePhtml); chmod(MODULES_PATH . ucfirst($this->getModule()) . DS . 'view' . DS . strtolower($this->getModule()) . DS . strtolower($this->getController()) . DS . 'index.phtml', 0644); $datagrid_template_trash = file_get_contents(dirname(__FILE__) . DS . 'Datagrid' . DS . 'Template_Datagrid_Trash.tpl'); $datagridTemplateTrash = str_replace("%moduleName%", $moduleName, str_replace("%moduleNameLower%", $moduleNameLower, $datagrid_template_trash)); file_put_contents(MODULES_PATH . ucfirst($this->getModule()) . DS . 'view' . DS . strtolower($this->getModule()) . DS . strtolower($this->getController()) . DS . 'trash.phtml', $datagridTemplateTrash); chmod(MODULES_PATH . ucfirst($this->getModule()) . DS . 'view' . DS . strtolower($this->getModule()) . DS . strtolower($this->getController()) . DS . 'trash.phtml', 0644); } } catch (Exception $exc) { throw new Exception('Não foi possivel criar o arquivo de configuração do Datagrid! <br />' . $exc->getMessage(), 500); } }
/** * Returns the column descriptions for a table. * * The return value is an associative array keyed by the column name, * as returned by the RDBMS. * * The value of each array element is an associative array * with the following keys: * * SCHEMA_NAME => string; name of database or schema * TABLE_NAME => string; * COLUMN_NAME => string; column name * COLUMN_POSITION => number; ordinal position of column in table * DATA_TYPE => string; SQL datatype name of column * DEFAULT => string; default expression of column, null if none * NULLABLE => boolean; true if column can have nulls * LENGTH => number; length of CHAR/VARCHAR * SCALE => number; scale of NUMERIC/DECIMAL * PRECISION => number; precision of NUMERIC/DECIMAL * UNSIGNED => boolean; unsigned property of an integer type * PRIMARY => boolean; true if column is part of the primary key * PRIMARY_POSITION => integer; position of column in primary key * IDENTITY => integer; true if column is auto-generated with unique values * * @param string $tableName * @param string $schemaName OPTIONAL * @return array */ public function describeTable($tableName, $schemaName = null) { $desc = []; $ddl = new \Zend\Db\Metadata\Metadata($this); $ddlTable = $ddl->getTable($tableName, $schemaName); $constraints = $ddlTable->getConstraints(); foreach ($ddlTable->getColumns() as $ddlColumn) { $primary = false; foreach ($constraints as $constraint) { $primary = in_array($ddlColumn->getName(), $constraint->getColumns()) && $constraint->isPrimaryKey(); } $desc[$ddlColumn->getName()] = array('SCHEMA_NAME' => null, 'TABLE_NAME' => $tableName, 'COLUMN_NAME' => $ddlColumn->getName(), 'COLUMN_POSITION' => $ddlColumn->getOrdinalPosition(), 'DATA_TYPE' => $ddlColumn->getDataType(), 'DEFAULT' => $ddlColumn->getColumnDefault(), 'NULLABLE' => $ddlColumn->isNullable(), 'LENGTH' => $ddlColumn->getCharacterMaximumLength(), 'SCALE' => $ddlColumn->getNumericScale(), 'PRECISION' => $ddlColumn->getNumericPrecision(), 'UNSIGNED' => $ddlColumn->isNumericUnsigned(), 'PRIMARY' => $primary, 'PRIMARY_POSITION' => 0, 'IDENTITY' => false); } return $desc; }
public function getForeignKey() { if ($this->getServiceLocator()->has('service_cache') && $this->getServiceLocator()->get('cache_metadata')['mode'] == 'enabled') { $this->foreign_key = $this->getCache()->getItem($this->table . '_ForeignKey'); if (!empty($this->foreign_key) || $this->getCache()->hasItem($this->table . '_ForeignKey')) { return $this->foreign_key; } } $metaData = new \Zend\Db\Metadata\Metadata($this->getAdapter()->getAdapter()); $table = $metaData->getTable($this->table); $contraints = $table->getConstraints(); foreach ($contraints as $contraint) { if ($contraint->getType() == self::FOREIGN_KEY) { $this->foreign_key[implode("_", $contraint->getColumns())] = array("column" => $contraint->getColumns(), "tableref" => $contraint->getReferencedTableName(), "columnref" => $contraint->getReferencedColumns()); } } if ($this->getServiceLocator()->has('service_cache') && $this->getServiceLocator()->get('cache_metadata')['mode'] == 'enabled') { $this->getCache()->setItem($this->table . '_ForeignKey', $this->foreign_key); # Tag the key when saving if (method_exists($this->getCache(), 'setTags')) { $this->getCache()->setTags($this->table . '_ForeignKey', array($this->table)); } } return $this->foreign_key; }
/** */ public function generateDbMapper() { $metadata = new \Zend\Db\Metadata\Metadata($this->adapter); // get the table names $tableNames = $metadata->getTableNames(); echo "<pre>"; foreach ($tableNames as $table) { echo '<br>Creating<strong> ' . $table . "</strong> "; $this->generateDirectoryPath(); $this->generateClassFile($table, $metadata->getTable($table)->getColumns()); } echo '<hr>---'; }
/** * Converts dtabase content to UTF-8 * * @param string $prefix Specified table prefix * @param string $table the table we want to convert datas from * * @return void */ private function _convertContentToUTF($prefix, $table) { try { $query = 'SET NAMES latin1'; $this->_db->query($query, Adapter::QUERY_MODE_EXECUTE); } catch (\Exception $e) { Analog::log('Cannot SET NAMES on table `' . $table . '`. ' . $e->getMessage(), Analog::ERROR); } try { $metadata = new \Zend\Db\Metadata\Metadata($this->_db); $tbl = $metadata->getTable($table); $columns = $tbl->getColumns(); $constraints = $tbl->getConstraints(); $pkeys = array(); foreach ($constraints as $constraint) { if ($constraint->getType() === 'PRIMARY KEY') { $pkeys = $constraint->getColumns(); } } if (count($pkeys) == 0) { //no primary key! How to do an update without that? //Prior to 0.7, l10n and dynamic_fields tables does not //contains any primary key. Since encoding conversion is done //_before_ the SQL upgrade, we'll have to manually //check these ones if (preg_match('/' . $prefix . 'dynamic_fields/', $table) !== 0) { $pkeys = array('item_id', 'field_id', 'field_form', 'val_index'); } else { if (preg_match('/' . $prefix . 'l10n/', $table) !== 0) { $pkeys = array('text_orig', 'text_locale'); } else { //not a know case, we do not perform any update. throw new \Exception('Cannot define primary key for table `' . $table . '`, aborting'); } } } $select = $this->_sql->select($table); $results = $this->execute($select); foreach ($results as $row) { $data = array(); $where = array(); //build where foreach ($pkeys as $k) { $where[] = $k . ' = "' . $row->{$k} . '"'; } //build data foreach ($row as $key => $value) { $data[$key] = $value; } //finally, update data! $update = $this->_sql->update($table); $update->set($data)->where($where); $this->execute($update); } } catch (\Exception $e) { Analog::log('An error occured while converting contents to UTF-8 for table ' . $table . ' (' . $e->getMessage() . ')', Analog::ERROR); } }
/** * @desc Zwraca tylko nazwy kolumn danej tabeli * @param $sTabelaSlownikowa * @return array */ public static function KolumnyNazwy($nazwaSlownika) { $metadata = new \Zend\Db\Metadata\Metadata(\Zend\Db\TableGateway\Feature\GlobalAdapterFeature::getStaticAdapter()); $table = $metadata->getTable($nazwaSlownika); $aColumns = $table->getColumns(); foreach ($aColumns as $oColumn) { $aColumnsName[] = $oColumn->getName(); } return $aColumnsName; }
public function getColumnsArray() { $aColumns = array(); $metadata = new \Zend\Db\Metadata\Metadata($this->adapter); $table = $metadata->getTable($this->getTable()); $aColumnsList = $table->getColumns(); foreach ($aColumnsList as $oColumn) { $aColumns[$oColumn->getName()] = ''; } return $aColumns; }
<?php $adapter = (include file_exists('bootstrap.php') ? 'bootstrap.php' : 'bootstrap.dist.php'); refresh_data($adapter); $metadata = new Zend\Db\Metadata\Metadata($adapter); // get the table names $tableNames = $metadata->getTableNames(); foreach ($tableNames as $tableName) { echo 'In Table ' . $tableName . PHP_EOL; $table = $metadata->getTable($tableName); echo ' With columns: ' . PHP_EOL; foreach ($table->getColumns() as $column) { echo ' ' . $column->getName() . ' -> ' . $column->getDataType() . PHP_EOL; } echo PHP_EOL; echo ' With constraints: ' . PHP_EOL; foreach ($metadata->getConstraints($tableName) as $constraint) { /** @var $constraint Zend\Db\Metadata\Object\ConstraintObject */ echo ' ' . $constraint->getName() . ' -> ' . $constraint->getType() . PHP_EOL; if (!$constraint->hasColumns()) { continue; } echo ' column: ' . implode(', ', $constraint->getColumns()); if ($constraint->isForeignKey()) { $fkCols = array(); foreach ($constraint->getReferencedColumns() as $refColumn) { $fkCols[] = $constraint->getReferencedTableName() . '.' . $refColumn; } echo ' => ' . implode(', ', $fkCols); } echo PHP_EOL;