Skip to content

Commit

Permalink
[7.x] Fix error with Has Many fieldtype when used on entries (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Sep 17, 2024
1 parent 33e4267 commit 03e341f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
25 changes: 11 additions & 14 deletions src/Fieldtypes/BaseFieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,20 @@ public function preload()

private function getUnlinkBehavior(): string
{
if (! $this->field->parent() || $this instanceof BelongsToFieldtype) {
return 'unlink';
}

$relationshipName = $this->config('relationship_name') ?? $this->field->handle();
$relationship = $this->field->parent()->{$relationshipName}();
if ($this->field->parent() instanceof Model && $this instanceof HasManyFieldtype) {
$relationshipName = $this->config('relationship_name') ?? $this->field->handle();
$relationship = $this->field->parent()->{$relationshipName}();
if ($relationship instanceof HasMany) {
$foreignKey = $relationship->getQualifiedForeignKeyName();

if ($relationship instanceof HasMany) {
$foreignKey = $relationship->getQualifiedForeignKeyName();
$foreignTable = explode('.', $foreignKey)[0];
$foreignColumn = explode('.', $foreignKey)[1];

$foreignTable = explode('.', $foreignKey)[0];
$foreignColumn = explode('.', $foreignKey)[1];
$column = collect(Schema::getColumns($foreignTable))
->first(fn (array $column) => $column['name'] === $foreignColumn);

$column = collect(Schema::getColumns($foreignTable))
->first(fn (array $column) => $column['name'] === $foreignColumn);

return Arr::get($column, 'nullable', false) ? 'unlink' : 'delete';
return Arr::get($column, 'nullable', false) ? 'unlink' : 'delete';
}
}

return 'unlink';
Expand Down
24 changes: 23 additions & 1 deletion tests/Fieldtypes/HasManyFieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use Illuminate\Support\Facades\Schema;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Blink;
use Statamic\Facades\Entry;
use Statamic\Fields\Field;
use Statamic\Http\Requests\FilteredRequest;
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
use StatamicRadPack\Runway\Fieldtypes\HasManyFieldtype;
use StatamicRadPack\Runway\Runway;
use StatamicRadPack\Runway\Tests\Fixtures\Models\Author;
Expand All @@ -19,7 +21,7 @@

class HasManyFieldtypeTest extends TestCase
{
use WithFaker;
use PreventsSavingStacheItemsToDisk, WithFaker;

protected HasManyFieldtype $fieldtype;

Expand Down Expand Up @@ -88,6 +90,26 @@ public function unlink_behavior_is_delete_when_relationship_column_is_not_nullab
$this->assertEquals('delete', $fieldtype->preload()['unlinkBehavior']);
}

#[Test]
public function unlink_behavior_is_unlink_when_field_is_used_on_entry()
{
\Statamic\Facades\Collection::make('pages')->save();

$field = new Field('posts', [
'mode' => 'stack',
'resource' => 'post',
'display' => 'Posts',
'type' => 'has_many',
]);

$field->setParent(Entry::make()->collection('pages'));

$fieldtype = new HasManyFieldtype;
$fieldtype->setField($field);

$this->assertEquals('unlink', $fieldtype->preload()['unlinkBehavior']);
}

#[Test]
public function can_get_index_items()
{
Expand Down

0 comments on commit 03e341f

Please sign in to comment.