Skip to content

Commit ea3b8db

Browse files
committed
Allow for foreign keys on pivot generation - resolves #274
1 parent 92789dd commit ea3b8db

File tree

6 files changed

+36
-37
lines changed

6 files changed

+36
-37
lines changed

spec/Way/Generators/Parsers/MigrationFieldsParserSpec.php

+10-14
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,28 @@ function it_is_initializable()
1515
function it_parses_a_string_of_fields()
1616
{
1717
$this->parse('name:string')->shouldReturn([
18-
'name' => ['type' => 'string']
18+
['field' => 'name', 'type' => 'string']
1919
]);
2020

2121
$this->parse('name:string, age:integer')->shouldReturn([
22-
'name' => ['type' => 'string'],
23-
'age' => ['type' => 'integer']
24-
]);
25-
26-
$this->parse('name:string, age:integer')->shouldReturn([
27-
'name' => ['type' => 'string'],
28-
'age' => ['type' => 'integer']
22+
['field' => 'name', 'type' => 'string'],
23+
['field' => 'age', 'type' => 'integer']
2924
]);
3025

3126
$this->parse('name:string:nullable, age:integer')->shouldReturn([
32-
'name' => ['type' => 'string', 'decorators' => ['nullable']],
33-
'age' => ['type' => 'integer']
27+
['field' => 'name', 'type' => 'string', 'decorators' => ['nullable']],
28+
['field' => 'age', 'type' => 'integer']
3429
]);
3530

3631
$this->parse('name:string(15):nullable')->shouldReturn([
37-
'name' => ['type' => 'string', 'args' => '15', 'decorators' => ['nullable']],
32+
['field' => 'name', 'type' => 'string', 'args' => '15', 'decorators' => ['nullable']]
3833
]);
3934

40-
$this->parse('column:double(15,8):nullable:default(10), age:integer')->shouldReturn([
41-
'column' => ['type' => 'double', 'args' => '15,8', 'decorators' => ['nullable', 'default(10)']],
42-
'age' => ['type' => 'integer']
35+
$this->parse('name:double(15,8):nullable:default(10), age:integer')->shouldReturn([
36+
['field' => 'name', 'type' => 'double', 'args' => '15,8', 'decorators' => ['nullable', 'default(10)']],
37+
['field' => 'age', 'type' => 'integer']
4338
]);
39+
4440
}
4541

4642
}

src/Way/Generators/Commands/PivotGeneratorCommand.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public function getMigrationFields($tableOne, $tableTwo)
5858
{
5959
return implode(', ', [
6060
"{$tableOne}_id:integer:unsigned:index",
61-
"{$tableTwo}_id:integer:unsigned:index"
61+
"{$tableOne}_id:foreign:references('id'):on('" . str_plural($tableOne) . "'):onDelete('cascade')",
62+
"{$tableTwo}_id:integer:unsigned:index",
63+
"{$tableTwo}_id:foreign:references('id'):on('" . str_plural($tableTwo) . "'):onDelete('cascade')",
6264
]);
6365
}
6466

src/Way/Generators/Parsers/MigrationFieldsParser.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ public function parse($fields)
1616
// name:string, age:integer
1717
// name:string(10,2), age:integer
1818
$fields = preg_split('/\s?,\s/', $fields);
19+
1920
$parsed = [];
2021

21-
foreach($fields as $field)
22+
foreach($fields as $index => $field)
2223
{
2324
// Example:
2425
// name:string:nullable => ['name', 'string', 'nullable']
@@ -45,10 +46,10 @@ public function parse($fields)
4546
// be our decorators
4647
$decorators = $chunks;
4748

48-
$parsed[$property] = ['type' => $type];
49+
$parsed[$index] = ['field' => $property, 'type' => $type];
4950

50-
if (isset($args)) $parsed[$property]['args'] = $args;
51-
if ($decorators) $parsed[$property]['decorators'] = $decorators;
51+
if (isset($args)) $parsed[$index]['args'] = $args;
52+
if ($decorators) $parsed[$index]['decorators'] = $decorators;
5253
}
5354

5455
return $parsed;

src/Way/Generators/Syntax/AddToTable.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ protected function addColumns($fields)
3131
{
3232
$schema = [];
3333

34-
foreach($fields as $property => $details)
34+
foreach($fields as $field)
3535
{
36-
$schema[] = $this->addColumn($property, $details);
36+
$schema[] = $this->addColumn($field);
3737
}
3838

3939
return $schema;
@@ -42,13 +42,13 @@ protected function addColumns($fields)
4242
/**
4343
* Return string for adding a column
4444
*
45-
* @param $property
46-
* @param $details
45+
* @param $field
4746
* @return string
4847
*/
49-
private function addColumn($property, $details)
48+
private function addColumn($field)
5049
{
51-
$type = $details['type'];
50+
$property = $field['field'];
51+
$type = $field['type'];
5252

5353
$output = sprintf(
5454
"\$table->%s(%s)",
@@ -58,19 +58,19 @@ private function addColumn($property, $details)
5858

5959
// If we have args, then it needs
6060
// to be formatted a bit differently
61-
if (isset($details['args']))
61+
if (isset($field['args']))
6262
{
6363
$output = sprintf(
6464
"\$table->%s('%s', %s)",
6565
$type,
6666
$property,
67-
$details['args']
67+
$field['args']
6868
);
6969
}
7070

71-
if (isset($details['decorators']))
71+
if (isset($field['decorators']))
7272
{
73-
$output .= $this->addDecorators($details['decorators']);
73+
$output .= $this->addDecorators($field['decorators']);
7474
}
7575

7676
return $output . ';';

src/Way/Generators/Syntax/CreateTable.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ public function create($migrationData, $fields)
1616

1717
// All new tables should have an identifier
1818
// Let's add that for the user automatically
19-
$primaryKey['id'] = ['type' => 'increments'];
20-
$fields = $primaryKey + $fields;
19+
array_unshift($fields, ['field' => 'id', 'type' => 'increments']);
2120

2221
// We'll also add timestamps to new tables for convenience
23-
$fields[''] = ['type' => 'timestamps'];
22+
array_push($fields, ['field' => '', 'type' => 'timestamps']);
2423

2524
return (new AddToTable($this->file, $this->compiler))->add($migrationData, $fields);
2625
}

src/Way/Generators/Syntax/RemoveFromTable.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ protected function dropColumns(array $fields)
2828
{
2929
$schema = [];
3030

31-
foreach($fields as $property => $type)
31+
foreach($fields as $field)
3232
{
33-
$schema[] = $this->dropColumn($property);
33+
$schema[] = $this->dropColumn($field);
3434
}
3535

3636
return $schema;
@@ -39,12 +39,13 @@ protected function dropColumns(array $fields)
3939
/**
4040
* Return string for dropping a column
4141
*
42-
* @param $property
42+
* @param $field
43+
* @internal param $property
4344
* @return string
4445
*/
45-
private function dropColumn($property)
46+
private function dropColumn($field)
4647
{
47-
return sprintf("\$table->dropColumn('%s');", $property);
48+
return sprintf("\$table->dropColumn('%s');", $field['field']);
4849
}
4950

5051
}

0 commit comments

Comments
 (0)