Skip to content

Commit

Permalink
Fixes yiisoft#162: Model generator now detects foreign keys named as …
Browse files Browse the repository at this point in the history
…`id_*`
  • Loading branch information
mootensai authored and samdark committed Jun 15, 2016
1 parent ad215b7 commit 2385e71
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Yii Framework 2 gii extension Change Log
- Bug #149: Relation names no longer override existing methods and properties (Faryshta)
- Bug #152: Fixed generating model without any rules (and800)
- Enh #153: Added filename filter to generated files list preview (thiagotalma)
- Enh #162: Model generator now detects foreign keys named as `id_*` (mootensai, samdark)


2.0.5 March 18, 2016
Expand Down
8 changes: 6 additions & 2 deletions generators/model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,12 @@ protected function generateRelationName($relations, $table, $key, $multiple)
$baseClass = $this->baseClass;
$baseModel = new $baseClass();
}
if (!empty($key) && substr_compare($key, 'id', -2, 2, true) === 0 && strcasecmp($key, 'id')) {
$key = rtrim(substr($key, 0, -2), '_');
if (!empty($key) && strcasecmp($key, 'id')) {
if (substr_compare($key, 'id', -2, 2, true) === 0) {
$key = rtrim(substr($key, 0, -2), '_');
} elseif (substr_compare($key, 'id', 0, 2, true) === 0) {
$key = ltrim(substr($key, 2, strlen($key)), '_');
}
}
if ($multiple) {
$key = Inflector::pluralize($key);
Expand Down

0 comments on commit 2385e71

Please sign in to comment.