doctrine/doctrine2 — pull request #357 by bpanahij (2012-05-25 19:54:12+00:00)
Title: Persistent and Called First ClassName

--Allow the use of the ClassName in Naming functions other than just setting the table name

I am using it to add a table name prefix to all of my columns within the table;
as follows:

className = $className;

$className = 'AdverseActionNaming';
$capString = preg_replace('/[^A-Z]/','',$className);
if(strlen($capString) < 4)
{
$capString = substr($capString,0,1).substr($className,1,4-strlen($capString)).substr($capString,1,3);
}

$this->prefix = strtoupper($capString);
}

public function classToTableName($className)
{
$wordArray = preg_split('/(?=\p{Lu})/u', $className);
$table_name_body = '';
$count = 0;
$word = "";
foreach($wordArray as $word)
{
$table_name_body .= '_'.strtoupper($word);
$count++;
}
return $this->prefix.$table_name_body;
}

public function propertyToColumnName($propertyName)
{
$wordArray = preg_split('/(?=\p{Lu})/u', $propertyName);
$column_name_body = '';
foreach($wordArray as $word)
{
$column_name_body .= '_'.strtoupper($word);
}
return $this->prefix.$column_name_body;
}

public function referenceColumnName()
{
return $this->prefix.'_ID';
}

public function joinColumnName($propertyName)
{
$table_name_parts = preg_split('/_/',$this->joinTableName());
return $this->prefix .'_' . $table_name_parts[0] . '_' . $this->referenceColumnName();
}

public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
{
return strtoupper($this->classToTableName($sourceEntity) . '_' .
$this->classToTableName($targetEntity));
}

public function joinKeyColumnName($entityName, $referencedColumnName = null)
{
return strtoupper($this->classToTableName($entityName) . '_' .
($referencedColumnName ?: $this->referenceColumnName()));
}
}