/** * Parse and process the structure content. * * create => create the table. * * add => add a new field. * update => make some changes into one field. * delete => delete a field. * * drop => drop the table. * * @param array $array Array from the json data with the table data. * * @return void */ private function _processStructure($array) { foreach ($array as $tableName => $content) { foreach ($content as $action => $fields) { switch ($action) { case 'create': if (!$this->_tableManager->tableExists($tableName)) { $keys = $this->_getKeys($fields); $fields = $this->_convertFieldsData($fields); $this->_tableManager->createTable($tableName, $fields, $keys); } break; case 'drop': $this->_tableManager->dropTable($tableName); break; case 'add': $fields = $this->_convertFieldsData($fields); foreach ($fields as $key => $field) { $this->_tableManager->addField($tableName, $field); } break; case 'update': $fields = $this->_convertFieldsData($fields); foreach ($fields as $key => $field) { if (!isset($field['newName'])) { $this->_tableManager->modifyField($tableName, $field); } else { $this->_tableManager->changeField($tableName, $field); } } break; case 'delete': $fields = $this->_convertFieldsData($fields); foreach ($fields as $key => $field) { $this->_tableManager->deleteField($tableName, $field); } break; case 'createIndex': if (!array_key_exists('columns', $fields)) { throw new Exception('Mandatory entry "columns" not found.'); } $options = array(); if (array_key_exists('name', $fields)) { $options['name'] = $fields['name']; } if (array_key_exists('unique', $fields)) { $options['unique'] = $fields['unique']; } $this->_tableManager->createIndex($tableName, $fields['columns'], $options); break; } } } }
/** * Parse and process the structure content. * * create => create the table. * * add => add a new field. * update => make some changes into one field. * delete => delete a field. * * drop => drop the table. * * @param array $array Array from the json data with the table data. * * @return void */ private function _processStructure($array) { foreach ($array as $tableName => $content) { foreach ($content as $action => $fields) { switch ($action) { case 'create': if (!$this->_tableManager->tableExists($tableName)) { $keys = $this->_getKeys($fields); $fields = $this->_convertFieldsData($fields); $this->_tableManager->createTable($tableName, $fields, $keys); } break; case 'drop': $this->_tableManager->dropTable($tableName); break; case 'add': $fields = $this->_convertFieldsData($fields); foreach ($fields as $key => $field) { $this->_tableManager->addField($tableName, $field); } break; case 'update': $fields = $this->_convertFieldsData($fields); foreach ($fields as $key => $field) { if (!isset($field['newName'])) { $this->_tableManager->modifyField($tableName, $field); } else { $this->_tableManager->changeField($tableName, $field); } } break; case 'delete': $fields = $this->_convertFieldsData($fields); foreach ($fields as $key => $field) { $this->_tableManager->deleteField($tableName, $field); } break; } } } }
/** * Check the current Fields and make the sync in the table of the module. * * @param array $newFields Array with all the data per new field. * @param string $tableName Name of the module Table. * @param array $tableData Array with the table data definition per new field. * * @return boolean True on a sucessful sync. */ public function syncTable($newFields, $tableName, $tableData) { $systemFields = array('id', 'owner_id', 'project_id'); $tableManager = new Phprojekt_Table(Phprojekt::getInstance()->getDb()); // Clean the metadata cache if (null !== $this->_model) { $info = $this->_model->info(); $dbConfig = Phprojekt::getInstance()->getDb()->getConfig(); // Define the cache identifier where the metadata are saved $cacheId = md5((isset($dbConfig['options']['port']) ? ':' . $dbConfig['options']['port'] : null) . (isset($dbConfig['options']['host']) ? ':' . $dbConfig['options']['host'] : null) . '/' . $dbConfig['dbname'] . ':' . $info['schema'] . '.' . $info['name']); Zend_Db_Table_Abstract::getDefaultMetadataCache()->remove($cacheId); } $oldFields = $this->getDataDefinition(); $tableDataForCreate['id'] = array('type' => 'auto_increment', 'length' => 11); $tableDataForCreate['owner_id'] = array('type' => 'int', 'length' => 11); if (!isset($tableDataForCreate['project_id'])) { $tableDataForCreate['project_id'] = array('type' => 'int', 'length' => 11); } array_merge($tableDataForCreate, $tableData); $tableName = strtolower(self::convertTableField($tableName)); $tableFields = $tableManager->getTableFields($tableName, $tableDataForCreate); // Search for Modify and Delete $return = true; foreach ($oldFields as $oldValues) { $found = false; foreach ($newFields as $newValues) { if ($oldValues['id'] == $newValues['id']) { $newValues['tableField'] = self::convertTableField($newValues['tableField']); $fieldDefinition = $tableData[$newValues['tableField']]; $fieldDefinition['name'] = $newValues['tableField']; if (!in_array($fieldDefinition['name'], $systemFields)) { if ($oldValues['tableField'] == $newValues['tableField']) { if (!$tableManager->modifyField($tableName, $fieldDefinition)) { $return = false; } } else { $fieldDefinition['oldName'] = $oldValues['tableField']; if (!$tableManager->changeField($tableName, $fieldDefinition)) { $return = false; } } } $found = true; break; } } if (!$found) { $fieldDefinition = array(); $fieldDefinition['name'] = $oldValues['tableField']; if (!in_array($fieldDefinition['name'], $systemFields)) { if (!$tableManager->deleteField($tableName, $fieldDefinition)) { $return = false; } } } } // Search for Add foreach ($newFields as $newValues) { if ($newValues['id'] == 0) { $newValues['tableField'] = self::convertTableField($newValues['tableField']); $fieldDefinition = $tableData[$newValues['tableField']]; $fieldDefinition['name'] = $newValues['tableField']; if (!in_array($fieldDefinition['name'], $systemFields)) { if (!$tableManager->addField($tableName, $fieldDefinition)) { $return = false; } } } } return $return; }