Repository: statamic-rad-pack/runway (php)

Issue:
Editing an entry with a BelongsTo field causes an error.
### Description

References: 
[Runway resource brand](https://github.com/rapidez/statamic/blob/master/resources/blueprints/runway/brand.yaml)
[Brand content collection](https://github.com/rapidez/statamic/blob/master/resources/blueprints/collections/brands/brand.yaml)

I have a collection "Brand content" and a Runway resources "Brands". Brand content has a BelongsTo field to the Brands Runway resource. This field is called `linked_brand`.  `linked_brand` is automaticly linked to a brand via a command. It is set in the database. However the field causes this error in the backend:

![Scherm­afbeelding 2024-09-17 om 13 43 36](https://github.com/user-attachments/assets/f0366d7a-5620-4406-8cbc-d1ec9fcb1e64)

When I add `return 'unlink'` [here](https://github.com/statamic-rad-pack/runway/blob/7.x/src/Fieldtypes/BaseFieldtype.php#L116) the page does show as follows:
![Scherm­afbeelding 2024-09-17 om 13 45 03](https://github.com/user-attachments/assets/ced5cb79-693a-430a-a714-b9acf36c95e4)


### Steps to reproduce

1. Add runway resource
2. Add collection with a runway BelongsTo field
3. Set the value of this field in the database/file
4. Edit the entry in the CP

### Environment


Environment
Application Name:
Laravel Version: 11.23.5
PHP Version: 8.2.23
Composer Version: 2.7.1
Environment: local
Debug Mode: ENABLED
URL:
Maintenance Mode: OFF
Timezone: Europe/Amsterdam
Locale: de

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: file
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Sentry
Enabled: MISSING DSN
Environment: local
Laravel SDK Version: 4.8.0
PHP SDK Version: 4.9.0
Release: 939b1c7f
Sample Rate Errors: 100%
Sample Rate Performance Monitoring: 0%
Sample Rate Profiling: NOT SET
Send Default PII: DISABLED

Statamic
Addons: 10
Sites: 10
Stache Watcher: Enabled
Static Caching: full
Version: 5.25.0 PRO

Statamic Addons
alt-design/alt-redirect: dev-main
eminos/statamic-iconify: 0.0.1
eminos/statamic-tabs: 1.2.0
just-better/statamic-authentik: 1.0.7
justbetter/statamic-eloquent-driver-globalset-migration-generator: 0.1.0
justbetter/statamic-image-optimize: 3.1.0
ryanmitchell/statamic-translation-manager: 1.6.0
spatie/statamic-responsive-images: 5.0.1
statamic-rad-pack/runway: 7.9.2
statamic/eloquent-driver: 4.14.3

Statamic Eloquent Driver
Asset Containers: file
Assets: eloquent
Blueprints: file
Collection Trees: file
Collections: file
Entries: eloquent
Forms: file
Global Sets: file
Global Variables: eloquent
Navigation Trees: eloquent
Navigations: file
Revisions: eloquent
Sites: file
Taxonomies: file
Terms: eloquent
Tokens: file



Pull request description:
[7.x] Fix error with Has Many fieldtype when used on entries
This pull request fixes an issue where an error would occur when using the Has Many fieldtype on an entry blueprint.

Closes #600.

Patch:
diff --git a/src/Fieldtypes/BaseFieldtype.php b/src/Fieldtypes/BaseFieldtype.php
index bcc2200..20f5105 100644
--- a/src/Fieldtypes/BaseFieldtype.php
+++ b/src/Fieldtypes/BaseFieldtype.php
@@ -114,23 +114,20 @@ class BaseFieldtype extends Relationship
 
     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';


Test patch:
diff --git a/tests/Fieldtypes/HasManyFieldtypeTest.php b/tests/Fieldtypes/HasManyFieldtypeTest.php
index 0f4e806..9aa9cad 100644
--- a/tests/Fieldtypes/HasManyFieldtypeTest.php
+++ b/tests/Fieldtypes/HasManyFieldtypeTest.php
@@ -9,8 +9,10 @@ use Illuminate\Support\Facades\Config;
 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;
@@ -19,7 +21,7 @@ use StatamicRadPack\Runway\Tests\TestCase;
 
 class HasManyFieldtypeTest extends TestCase
 {
-    use WithFaker;
+    use PreventsSavingStacheItemsToDisk, WithFaker;
 
     protected HasManyFieldtype $fieldtype;
 
@@ -88,6 +90,26 @@ class HasManyFieldtypeTest extends TestCase
         $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()
     {