Skip to content

Commit

Permalink
Merge pull request #16 from estebanprimost/master
Browse files Browse the repository at this point in the history
Primary Key, Table filter and use statement
  • Loading branch information
iberflow committed Feb 29, 2016
2 parents a87bc67 + 5ebad8b commit 9eb4d00
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Options:
- --timestamps="" Rules for $timestamps columns (default: "ends:_at")
- --ignore=""|-i="" A table names to ignore
- --ignoresystem|-s List of system tables (auth, migrations, entrust package)
- --tables="" Tables to generate (E.g.: --tables="my_db_table1,my_db_table2"

# Running the generator
```php artisan make:models```
Expand Down
52 changes: 47 additions & 5 deletions src/Commands/MakeModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MakeModelsCommand extends GeneratorCommand
*
* @var string
*/
protected $extends = 'Model';
protected $extends = 'Illuminate\Database\Eloquent\Model';

/**
* Rule processor class instance.
Expand Down Expand Up @@ -111,7 +111,15 @@ public function fire()
*/
protected function getSchemaTables()
{
$tables = \DB::select("SELECT table_name AS `name` FROM information_schema.tables WHERE table_schema = DATABASE()");
$filterTablesWhere = '';
if ($this->option("tables")) {
$tableNamesToFilter = explode(',', $this->option('tables'));
if(is_array($tableNamesToFilter) && count($tableNamesToFilter) > 0) {
$filterTablesWhere = ' AND table_name IN (\'' . implode('\', \'', $tableNamesToFilter) . '\')';
}
}

$tables = \DB::select("SELECT table_name AS `name` FROM information_schema.tables WHERE table_schema = DATABASE()" . $filterTablesWhere);

return $tables;
}
Expand Down Expand Up @@ -175,7 +183,14 @@ protected function replaceTokens($name, $table)

$properties = $this->getTableProperties($table);

$class = str_replace('{{extends}}', $this->option('extends'), $class);
$extends = $this->option('extends');

$class = str_replace('{{table}}', 'protected $table = \'' . $table . '\';', $class);

$class = str_replace('{{primaryKey}}', $properties['primaryKey'] ? ('protected $primaryKey = \'' . $properties['primaryKey'] . '\';' . "\r\n\r\n\t") : '', $class);

$class = str_replace('{{extends}}', $extends, $class);
$class = str_replace('{{shortNameExtends}}', explode('\\', $extends)[count(explode('\\', $extends))-1], $class);
$class = str_replace('{{fillable}}', 'protected $fillable = ' . VariableConversion::convertArrayToString($properties['fillable']) . ';', $class);
$class = str_replace('{{guarded}}', 'protected $guarded = ' . VariableConversion::convertArrayToString($properties['guarded']) . ';', $class);
$class = str_replace('{{timestamps}}', 'public $timestamps = ' . VariableConversion::convertBooleanToString($properties['timestamps']) . ';', $class);
Expand Down Expand Up @@ -227,6 +242,9 @@ protected function replaceTokensWithSetGetFunctions($properties, $class)
*/
protected function getTableProperties($table)
{
$primaryKey = $this->getTablePrimaryKey($table);
$primaryKey = $primaryKey != 'id' ? $primaryKey : null;

$fillable = [];
$guarded = [];
$timestamps = false;
Expand All @@ -237,7 +255,7 @@ protected function getTableProperties($table)

//priotitze guarded properties and move to fillable
if ($this->ruleProcessor->check($this->option('fillable'), $column->name)) {
if (!in_array($column->name, ['id', 'created_at', 'updated_at', 'deleted_at'])) {
if (!in_array($column->name, array_merge(['id', 'created_at', 'updated_at', 'deleted_at'], $primaryKey ? [ $primaryKey ] : []))) {
$fillable[] = $column->name;
}
}
Expand All @@ -250,7 +268,7 @@ protected function getTableProperties($table)
}
}

return ['fillable' => $fillable, 'guarded' => $guarded, 'timestamps' => $timestamps];
return ['primaryKey' => $primaryKey, 'fillable' => $fillable, 'guarded' => $guarded, 'timestamps' => $timestamps];
}

/**
Expand All @@ -267,6 +285,29 @@ protected function getTableColumns($table)
return $columns;
}

/**
* Get table primary key column.
*
* @param $table
*
* @return string
*/
protected function getTablePrimaryKey($table)
{
$primaryKeyResult = \DB::select(
"SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND
TABLE_NAME = '{$table}' AND
COLUMN_KEY = 'PRI'");

if (count($primaryKeyResult) == 1){
return $primaryKeyResult[0]->COLUMN_NAME;
}

return null;
}

/**
* Get stub file location.
*
Expand Down Expand Up @@ -295,6 +336,7 @@ protected function getArguments()
protected function getOptions()
{
return [
['tables', null, InputOption::VALUE_OPTIONAL, 'Comma separated table names to generate', null],
['dir', null, InputOption::VALUE_OPTIONAL, 'Model directory', $this->namespace],
['extends', null, InputOption::VALUE_OPTIONAL, 'Parent class', $this->extends],
['fillable', null, InputOption::VALUE_OPTIONAL, 'Rules for $fillable array columns', $this->fillableRules],
Expand Down
7 changes: 4 additions & 3 deletions src/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace DummyNamespace;

use Illuminate\Database\Eloquent\Model;
use {{extends}};

/**
* Class DummyClass
*/
class DummyClass extends {{extends}}
class DummyClass extends {{shortNameExtends}}
{
{{table}}

{{timestamps}}
{{primaryKey}}{{timestamps}}

{{fillable}}

Expand Down

0 comments on commit 9eb4d00

Please sign in to comment.