From 27a39bc33510e8eeeccb190a5ab3488aa9bb7dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ara=C3=BAjo?= Date: Wed, 27 Jan 2016 10:46:50 -0300 Subject: [PATCH 1/3] Putting the name of the relationship functions in lower case, the problem happens when the database tables are in upcase --- src/Console/GenerateModelsCommand.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Console/GenerateModelsCommand.php b/src/Console/GenerateModelsCommand.php index 1df8d98..32551f2 100644 --- a/src/Console/GenerateModelsCommand.php +++ b/src/Console/GenerateModelsCommand.php @@ -21,6 +21,7 @@ class GenerateModelsCommand extends GeneratorCommand protected $name = 'models:generate'; private static $namespace; + /** * The console command description. * @@ -228,8 +229,6 @@ private function getNamespace() { $ns = env('APP_NAME','App\Models'); } - //convert forward slashes in the namespace to backslashes - $ns = str_replace('/', '\\', $ns); return $ns; } @@ -256,7 +255,7 @@ private function generateHasManyFunctions($rulesContainer) $function = " public function $hasManyFunctionName() {".' - return $this->hasMany'."(\\".self::$namespace."\\$hasManyModel::class, '$key1', '$key2'); + return $this->hasMany'."('".self::$namespace."\\$hasManyModel', '$key1', '$key2'); } "; $functions .= $function; @@ -277,7 +276,7 @@ private function generateHasOneFunctions($rulesContainer) $function = " public function $hasOneFunctionName() {".' - return $this->hasOne'."(\\".self::$namespace."\\$hasOneModel::class, '$key1', '$key2'); + return $this->hasOne'."('".self::$namespace."\\$hasOneModel', '$key1', '$key2'); } "; $functions .= $function; @@ -298,7 +297,7 @@ private function generateBelongsToFunctions($rulesContainer) $function = " public function $belongsToFunctionName() {".' - return $this->belongsTo'."(\\".self::$namespace."\\$belongsToModel::class, '$key1', '$key2'); + return $this->belongsTo'."('".self::$namespace."\\$belongsToModel', '$key1', '$key2'); } "; $functions .= $function; @@ -320,7 +319,7 @@ private function generateBelongsToManyFunctions($rulesContainer) $function = " public function $belongsToManyFunctionName() {".' - return $this->belongsToMany'."(\\".self::$namespace."\\$belongsToManyModel::class, '$through', '$key1', '$key2'); + return $this->belongsToMany'."('".self::$namespace."\\$belongsToManyModel', '$through', '$key1', '$key2'); } "; $functions .= $function; @@ -332,13 +331,13 @@ public function $belongsToManyFunctionName() {".' private function getPluralFunctionName($modelName) { $modelName = lcfirst($modelName); - return str_plural($modelName); + return str_plural(mb_strtolower($modelName)); } private function getSingularFunctionName($modelName) { $modelName = lcfirst($modelName); - return str_singular($modelName); + return str_singular(mb_strtolower($modelName)); } private function generateModelNameFromTableName($table) From 11b02fe7e27d55dd29e7264371157f7f3159101f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ara=C3=BAjo?= Date: Wed, 27 Jan 2016 11:27:48 -0300 Subject: [PATCH 2/3] Adding the primary key in the model according to the database --- src/Console/GenerateModelsCommand.php | 14 ++++++++++++-- src/Console/templates/model.txt | 4 +++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Console/GenerateModelsCommand.php b/src/Console/GenerateModelsCommand.php index 32551f2..4dfa29d 100644 --- a/src/Console/GenerateModelsCommand.php +++ b/src/Console/GenerateModelsCommand.php @@ -155,6 +155,7 @@ private function generateEloquentModels($destinationFolder, $eloquentRules) private function generateEloquentModel($destinationFolder, $table, $rules) { + //1. Determine path where the file should be generated $modelName = $this->generateModelNameFromTableName($table); $filePathToGenerate = $destinationFolder . '/'.$modelName.'.php'; @@ -185,13 +186,22 @@ private function generateEloquentModel($destinationFolder, $table, $rules) { $hasOneFunctions, ]); + $keys = $this->schemaGenerator->getPrimaryKeys($table); + + if(sizeof($keys) == 1){ + $primaryKeys = "'".current($keys)."'"; + }else{ + $primaryKeys = "array('".implode(",",$keys)."')"; + } + //3. prepare template data $templateData = array( 'NAMESPACE' => self::$namespace, 'NAME' => $modelName, 'TABLENAME' => $table, 'FILLABLE' => $fillable, - 'FUNCTIONS' => $functions + 'FUNCTIONS' => $functions, + 'PRIMARYKEY' => $primaryKeys ); $templatePath = $this->getTemplatePath(); @@ -600,4 +610,4 @@ protected function getTemplatePath() return $tp; } -} +} \ No newline at end of file diff --git a/src/Console/templates/model.txt b/src/Console/templates/model.txt index 9791f15..7593e75 100644 --- a/src/Console/templates/model.txt +++ b/src/Console/templates/model.txt @@ -11,6 +11,8 @@ class $NAME$ extends Model { protected $table = '$TABLENAME$'; protected $fillable = [$FILLABLE$]; + protected $primaryKey = $PRIMARYKEY$; + $FUNCTIONS$ -} +} \ No newline at end of file From 26e56b9c7e86da100ffd2c3bdd08b2199fa895f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ara=C3=BAjo?= Date: Wed, 27 Jan 2016 11:51:54 -0300 Subject: [PATCH 3/3] fixar colunas de nomes para baixar --- src/Console/GenerateModelsCommand.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Console/GenerateModelsCommand.php b/src/Console/GenerateModelsCommand.php index 4dfa29d..29d03cd 100644 --- a/src/Console/GenerateModelsCommand.php +++ b/src/Console/GenerateModelsCommand.php @@ -258,8 +258,8 @@ private function generateHasManyFunctions($rulesContainer) $functions = ''; foreach ($rulesContainer as $rules) { $hasManyModel = $this->generateModelNameFromTableName($rules[0]); - $key1 = $rules[1]; - $key2 = $rules[2]; + $key1 = mb_strtolower($rules[1]); + $key2 = mb_strtolower($rules[2]); $hasManyFunctionName = $this->getPluralFunctionName($hasManyModel); @@ -279,8 +279,8 @@ private function generateHasOneFunctions($rulesContainer) $functions = ''; foreach ($rulesContainer as $rules) { $hasOneModel = $this->generateModelNameFromTableName($rules[0]); - $key1 = $rules[1]; - $key2 = $rules[2]; + $key1 = mb_strtolower($rules[1]); + $key2 = mb_strtolower($rules[2]); $hasOneFunctionName = $this->getSingularFunctionName($hasOneModel); @@ -300,8 +300,8 @@ private function generateBelongsToFunctions($rulesContainer) $functions = ''; foreach ($rulesContainer as $rules) { $belongsToModel = $this->generateModelNameFromTableName($rules[0]); - $key1 = $rules[1]; - $key2 = $rules[2]; + $key1 = mb_strtolower($rules[1]); + $key2 = mb_strtolower($rules[2]); $belongsToFunctionName = $this->getSingularFunctionName($belongsToModel); @@ -321,9 +321,9 @@ private function generateBelongsToManyFunctions($rulesContainer) $functions = ''; foreach ($rulesContainer as $rules) { $belongsToManyModel = $this->generateModelNameFromTableName($rules[0]); - $through = $rules[1]; - $key1 = $rules[2]; - $key2 = $rules[3]; + $through = mb_strtolower($rules[1]); + $key1 = mb_strtolower($rules[2]); + $key2 = mb_strtolower($rules[3]); $belongsToManyFunctionName = $this->getPluralFunctionName($belongsToManyModel); @@ -610,4 +610,3 @@ protected function getTemplatePath() return $tp; } -} \ No newline at end of file