/** * Create a safe table name from the input * * @param bool $isNew * @param array $data * @param FabrikTableList $item * * @return string */ private function safeTableName($isNew, $data, $item) { if ($isNew) { $dbTableName = $data['db_table_name'] !== '' ? $data['db_table_name'] : $data['label']; // Mysql will force db table names to lower case even if you set the db name to upper case - so use clean() $dbTableName = FabrikString::clean($dbTableName); // Otherwise part of the table name is taken for element names $dbTableName = str_replace('___', '_', $dbTableName); } else { $dbTableName = $item->get('db_table_name', '') == '' ? $data['database_name'] : $item->get('db_table_name'); } return preg_replace('#[^0-9a-zA-Z_]#', '', $dbTableName); }
/** * Called at the end of a list save. * Update the created joins with the created list's id and db_table_name * * @param FabrikTableList $row List data * * @return void */ public function finalise($row) { $source = $this->getSourceTableName(); $targetTable = $row->get('db_table_name'); foreach ($this->joinIds as $joinId) { $joinTable = FabTable::getInstance('Join', 'FabrikTable'); $joinTable->load($joinId); if ((int) $joinTable->get('element_id') === 0) { // Group join $joinTable->set('list_id', $row->get('id')); $joinTable->set('join_from_table', $targetTable); } else { // Element join $tableLookUps = array('join_from_table', 'table_join', 'table_join_alias'); foreach ($tableLookUps as $tableLookup) { if ($joinTable->get($tableLookup) === $source) { $joinTable->set($tableLookup, $targetTable); } } } $joinTable->store(); } // Update element params with source => target table name conversion foreach ($this->elementIds as $elementId) { /** @var FabrikTableElement $element */ $element = FabTable::getInstance('Element', 'FabrikTable'); $element->load($elementId); $elementParams = new Registry($element->params); if ($elementParams->get('join_db_name') === $source) { $elementParams->set('join_db_name', $targetTable); $element->set('params', $elementParams->toString()); $element->store(); } } }