Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fk column with code_ or _code #296

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions generators/model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Generator extends \yii\gii\Generator
public $queryNs = 'app\models';
public $queryClass;
public $queryBaseClass = 'yii\db\ActiveQuery';
public $fkColumnIdentifiers = ['id'];


/**
Expand Down Expand Up @@ -684,11 +685,13 @@ protected function generateRelationName($relations, $table, $key, $multiple)
$baseModel = new $baseClass();
$baseModel->setAttributes([]);
}
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)), '_');
foreach ($this->fkColumnIdentifiers as $str) {
if (!empty($key) && strcasecmp($key, $str)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's meant by strcasecmp here? Not equal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i think so.
Actually i just copy that line from if (!empty($key) && strcasecmp($key, 'id')) { and change id string into $str variable accordingly.

Is there anything that i should revise?

if (substr_compare($key, $str, strlen($str)*-1, strlen($str), true) === 0) {
$key = rtrim(substr($key, 0, strlen($str)*-1), '_');
} elseif (substr_compare($key, $str, 0, strlen($str), true) === 0) {
$key = ltrim(substr($key, strlen($str), strlen($key)), '_');
}
}
}
if ($multiple) {
Expand Down