From 3dc901427c9b787c8438cacd06e89210fbd963aa Mon Sep 17 00:00:00 2001 From: Adi Priyanto Date: Sat, 2 Sep 2017 11:55:57 +0700 Subject: [PATCH 1/2] support fk column with code_ or _code --- generators/model/Generator.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/generators/model/Generator.php b/generators/model/Generator.php index eb75ecbc9..8db1ee97c 100644 --- a/generators/model/Generator.php +++ b/generators/model/Generator.php @@ -684,11 +684,17 @@ 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)), '_'); + $string_replacements = [ + 'id', + 'code', + ]; + foreach ($string_replacements as $str) { + if (!empty($key) && strcasecmp($key, $str)) { + 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) { From d2c8890b33cd9ee4d8652669c7590a34690f8be2 Mon Sep 17 00:00:00 2001 From: Adi Priyanto Date: Sun, 10 Sep 2017 03:06:42 +0700 Subject: [PATCH 2/2] make fk column identifiers public property --- generators/model/Generator.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/generators/model/Generator.php b/generators/model/Generator.php index 8db1ee97c..7dea31b36 100644 --- a/generators/model/Generator.php +++ b/generators/model/Generator.php @@ -43,6 +43,7 @@ class Generator extends \yii\gii\Generator public $queryNs = 'app\models'; public $queryClass; public $queryBaseClass = 'yii\db\ActiveQuery'; + public $fkColumnIdentifiers = ['id']; /** @@ -684,11 +685,7 @@ protected function generateRelationName($relations, $table, $key, $multiple) $baseModel = new $baseClass(); $baseModel->setAttributes([]); } - $string_replacements = [ - 'id', - 'code', - ]; - foreach ($string_replacements as $str) { + foreach ($this->fkColumnIdentifiers as $str) { if (!empty($key) && strcasecmp($key, $str)) { if (substr_compare($key, $str, strlen($str)*-1, strlen($str), true) === 0) { $key = rtrim(substr($key, 0, strlen($str)*-1), '_');