Final Class Yiisoft\Yii\Gii\Generator\ActiveRecord\ArHelper
| Inheritance | Yiisoft\Yii\Gii\Generator\ActiveRecord\ArHelper |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| getRelationName() | Returns the relation name for the given column names. | Yiisoft\Yii\Gii\Generator\ActiveRecord\ArHelper |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| IDENTITY_ENDING_PATTERN | '/_(?:id|uuid|key|code)$/i' | Yiisoft\Yii\Gii\Generator\ActiveRecord\ArHelper | |
| IDENTITY_FIELD_NAMES | [ 'id', 'uuid', 'key', 'code', ] | Yiisoft\Yii\Gii\Generator\ActiveRecord\ArHelper |
Method Details
Returns the relation name for the given column names.
| public static string getRelationName ( string[] $columnNames, string $defaultName ) | ||
| $columnNames | string[] |
The column names. |
| $defaultName | string |
The default relation name to use if a column name is an identity field name (e.g. id, uuid, key, code). It can be a related table name or a related model name. |
public static function getRelationName(array $columnNames, string $defaultName): string
{
$names = [];
foreach ($columnNames as $columnName) {
if (in_array(strtolower($columnName), self::IDENTITY_FIELD_NAMES, true)) {
$names[] = $defaultName;
continue;
}
if (preg_match(self::IDENTITY_ENDING_PATTERN, $columnName, $matches) === 1) {
$names[] = substr($columnName, 0, -strlen($matches[0]));
continue;
}
$names[] = $columnName;
}
$inflector = new Inflector();
$name = implode(' ', $names);
$name = $inflector->toWords($name);
$name = strtolower($name);
return $inflector->toCamelCase($name);
}
Signup or Login in order to comment.