/** * checks whether $aliasText in $column is already present * if $aliasText is found, numeric increments are added to it * if $id is set, this record is left out from checking * returns string which is unique in $column * * currently works only with single-field primary keys * * @param DatabaseInterface $connection * @param string $aliasText * @param string $tableName * @param int $id * @param string $column * * @return string */ public static function getAlias(DatabaseInterface $connection, $aliasText, $tableName, $id = 0, $column = 'alias') { $replace = array('~(ä|ä)~' => 'ae', '~(ö|ö)~' => 'oe', '~(ü|ü)~' => 'ue', '~(ß|ß)~' => 'ss', '~\\W+~' => '_', '~(^_+|_+$)~' => ''); $primaryKeyName = $connection->getPrimaryKey($tableName); $alias = preg_replace(array_keys($replace), array_values($replace), strtolower($aliasText)); $statement = $connection->getConnection()->prepare('SELECT ' . $column . ' FROM ' . $tableName . ' WHERE LOWER(' . $column . ') LIKE ? AND ' . $primaryKeyName . ' != ?'); $statement->execute(array($alias . '%', $id)); $aliasValues = $statement->fetchAll(\PDO::FETCH_COLUMN, 0); if (count($aliasValues) === 0) { return $alias; } $ndx = 2; while (in_array($alias . '_' . $ndx, $aliasValues)) { ++$ndx; } return $alias . '_' . $ndx; }