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

Fix problem with the formatting of the name of the relationship call functions #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
46 changes: 27 additions & 19 deletions src/Console/GenerateModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class GenerateModelsCommand extends GeneratorCommand
protected $name = 'models:generate';

private static $namespace;

/**
* The console command description.
*
Expand Down Expand Up @@ -154,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';
Expand Down Expand Up @@ -184,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();
Expand Down Expand Up @@ -228,8 +239,6 @@ private function getNamespace() {
$ns = env('APP_NAME','App\Models');
}

//convert forward slashes in the namespace to backslashes
$ns = str_replace('/', '\\', $ns);
return $ns;

}
Expand All @@ -249,14 +258,14 @@ 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);

$function = "
public function $hasManyFunctionName() {".'
return $this->hasMany'."(\\".self::$namespace."\\$hasManyModel::class, '$key1', '$key2');
return $this->hasMany'."('".self::$namespace."\\$hasManyModel', '$key1', '$key2');
}
";
$functions .= $function;
Expand All @@ -270,14 +279,14 @@ 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);

$function = "
public function $hasOneFunctionName() {".'
return $this->hasOne'."(\\".self::$namespace."\\$hasOneModel::class, '$key1', '$key2');
return $this->hasOne'."('".self::$namespace."\\$hasOneModel', '$key1', '$key2');
}
";
$functions .= $function;
Expand All @@ -291,14 +300,14 @@ 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);

$function = "
public function $belongsToFunctionName() {".'
return $this->belongsTo'."(\\".self::$namespace."\\$belongsToModel::class, '$key1', '$key2');
return $this->belongsTo'."('".self::$namespace."\\$belongsToModel', '$key1', '$key2');
}
";
$functions .= $function;
Expand All @@ -312,15 +321,15 @@ 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);

$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;
Expand All @@ -332,13 +341,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)
Expand Down Expand Up @@ -601,4 +610,3 @@ protected function getTemplatePath()

return $tp;
}
}
4 changes: 3 additions & 1 deletion src/Console/templates/model.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class $NAME$ extends Model {
protected $table = '$TABLENAME$';
protected $fillable = [$FILLABLE$];

protected $primaryKey = $PRIMARYKEY$;

$FUNCTIONS$

}
}