Final Class Yiisoft\Yii\Gii\Generator\ActiveRecord\ArHelper
| Inheritance | Yiisoft\ |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| getRelationName() | Returns the relation name for the given column names. | Yiisoft\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| IDENTITY_ENDING_PATTERN | '/_(?:id|uuid|key|code)$/i' | Yiisoft\ |
|
| IDENTITY_FIELD_NAMES | [ 'id', 'uuid', 'key', 'code', ] | Yiisoft\ |
Method Details
Returns the relation name for the given column names.
| public static string getRelationName ( string[] $columnNames, string $defaultName, boolean $isSingular = true ) | ||
| $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. |
| $isSingular | boolean |
Whether the relation name should be singular. |
public static function getRelationName(array $columnNames, string $defaultName, bool $isSingular = true): 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);
$name = self::changeLastWordForm($name, $isSingular);
return $inflector->toCamelCase($name);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.