/** * Add an index to the table * * @param string $field field name * @param string $prefix index name prefix (allows you to differentiate between indexes created in * different parts of fabrik) * @param string $type index type * @param string $size index length * * @return void */ public function addIndex($field, $prefix = '', $type = 'INDEX', $size = '') { if (is_numeric($field)) { $el = $this->getFormModel()->getElement($field, true); $field = $el->getFullName(true, false); } /* $$$ hugh - @TODO $field is in 'table.element' format but $indexes * has Column_name as just 'element' ... so we're always rebuilding indexes! * I'm in the middle of fixing something else, must come back and fix this!! * OK, moved these two lines from below to here */ $field = str_replace('_raw', '', $field); // $$$ rob 29/03/2011 ensure its in tablename___elementname format $field = str_replace('.', '___', $field); // $$$ rob 28/02/2011 if index in joined table we need to use that the make the key on if (!strstr($field, '___')) { $table = $this->getTable()->db_table_name; } else { $fieldParts = explode('___', $field); $table = array_shift($fieldParts); } $field = FabrikString::shortColName($field); $indexes = $this->getIndexes($table); FArrayHelper::filter($indexes, 'Column_name', $field); if (!empty($indexes)) { // An index already exists on that column name no need to add return; } $db = $this->getDb(); if ($field == '') { return; } if ($size != '') { $size = '( ' . $size . ' )'; } $this->dropIndex($field, $prefix, $type, $table); $query = ' ALTER TABLE ' . $db->qn($table) . ' ADD INDEX ' . $db->qn("fb_{$prefix}_{$field}_{$type}") . ' (' . $db->qn($field) . ' ' . $size . ')'; $db->setQuery($query); try { $db->execute(); } catch (RuntimeException $e) { // Try to suppress error $this->setError($e->getMessage()); } }
/** * add an index to the table * @param string field name * @param stirng index name prefix (allows you to differentiate between indexes created in * different parts of fabrik) * @param string index type * @param int index length */ public function addIndex($field, $prefix = '', $type = 'INDEX', $size = '') { $indexes =& $this->getIndexes(); if (is_numeric($field)) { $el = $this->getFormModel()->getElement($field, true); $field = $el->getFullName(false, true, false); } // $$$ hugh - @TODO $field is in 'table.element' format but $indexes // has Column_name as just 'element' ... so we're always rebuilding indexes! // I'm in the middle of fixing something else, must come back and fix this!! // OK, moved these two lines from below to here $field = str_replace('_raw', '', $field); // $$$ rob 29/03/2011 ensure its in tablename___elementname format $field = str_replace('.', '___', $field); // $$$ rob 28/02/2011 if index in joined table we need to use that the make the key on if (!strstr($field, '___')) { $table = $this->getTable()->db_table_name; } else { $table = array_shift(explode('___', $field)); } $field = FabrikString::shortColName($field); FArrayHelper::filter($indexes, 'Column_name', $field); if (!empty($indexes)) { // an index already exists on that column name no need to add return; } $db = $this->getDb(); if ($field == '') { return; } if ($size != '') { $size = "( {$size} )"; } $this->dropIndex($field, $prefix, $type, $table); $query = " ALTER TABLE " . $db->nameQuote($table) . " ADD INDEX " . $db->nameQuote("fb_{$prefix}_{$field}_{$type}") . " (" . $db->nameQuote($field) . " {$size})"; $db->setQuery($query); $db->query(); }