Skip to content

Commit

Permalink
Return pivot generator - closes #274
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Mar 10, 2014
1 parent e1d1a32 commit be05dbb
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
51 changes: 51 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This Laravel 4 package provides a variety of generators to speed up your develop
- `generate:controller`
- `generate:seed`
- `generate:migration`
- `generate:pivot`
- `generate:resource`
- `generate:scaffold`

Expand Down Expand Up @@ -309,6 +310,56 @@ class UsersTableSeeder extends Seeder {

This will give you a basic bit of boilerplate, using the popular Faker library. This is a nice way to seed your DB tables. Don't forget to pull in Faker through Composer!

### Pivot Tables

When you require a new pivot table, the `generate:pivot` table expedites the process of creating the appropriate migration.

Simply pass the table of the two tables that require a join pivot table. For `orders` and `users`, you might do:

```bash
php artisan generate:pivot orders users
```

This will create the following migration:

```php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateOrderUserTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('order_user', function(Blueprint $table) {
$table->increments('id');
$table->integer('order_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('order_user');
}

}
```

Notice that it correctly sets the table name according to your two provided tables, in alphabetical order. Now, run `php artisan migrate` to create your pivot table!

### Resources

The `generate:resource` command will do a number of things for you:
Expand Down
81 changes: 81 additions & 0 deletions src/Way/Generators/Commands/PivotGeneratorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php namespace Way\Generators\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class PivotGeneratorCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'generate:pivot';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a pivot table';

/**
* Create a pivot table migration
*/
public function fire()
{
list($tableOne, $tableTwo) = $this->sortDesiredTables();

$this->call(
'generate:migration',
[
'migrationName' => "create_{$tableOne}_{$tableTwo}_table",
'--fields' => $this->getMigrationFields($tableOne, $tableTwo)
]
);
}

/**
* Sort the provided pivot tables in alphabetical order
*
* @return array
*/
public function sortDesiredTables()
{
$tables = array_except(array_map('str_singular', $this->argument()), 'command');

sort($tables);

return $tables;
}

/**
* Get the fields for the pivot migration
*
* @param $tableOne
* @param $tableTwo
* @return array
*/
public function getMigrationFields($tableOne, $tableTwo)
{
return implode(', ', [
"{$tableOne}_id:integer:unsigned:index",
"{$tableTwo}_id:integer:unsigned:index"
]);
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['tableOne', InputArgument::REQUIRED, 'Name of the first table'],
['tableTwo', InputArgument::REQUIRED, 'Name of the second table']
];
}

}
15 changes: 15 additions & 0 deletions src/Way/Generators/GeneratorsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Way\Generators\Commands\PublishTemplatesCommand;
use Way\Generators\Commands\ScaffoldGeneratorCommand;
use Way\Generators\Commands\ViewGeneratorCommand;
use Way\Generators\Commands\PivotGeneratorCommand;

class GeneratorsServiceProvider extends ServiceProvider {

Expand Down Expand Up @@ -40,6 +41,7 @@ public function register()
'Controller',
'Migration',
'Seeder',
'Pivot',
'Resource',
'Scaffold',
'Publisher'] as $command)
Expand Down Expand Up @@ -121,6 +123,19 @@ protected function registerSeeder()
$this->commands('generate.seeder');
}

/**
* Register the pivot generator
*/
protected function registerPivot()
{
$this->app['generate.pivot'] = $this->app->share(function($app)
{
return new PivotGeneratorCommand;
});

$this->commands('generate.pivot');
}

/**
* Register the resource generator
*/
Expand Down

0 comments on commit be05dbb

Please sign in to comment.