Iāll get the ball rolling here. Following on from the reddit post. Some research/notes here:
In the file modules/backend/behaviors/relationcontroller/HasManageMode.php the following code prevents duplicates from being selected:
// Exclude existing relationships
$widget->bindEvent('list.extendQuery', function ($query) {
// Where not in the current list of related records
$existingIds = $this->findExistingRelationIds();
if (count($existingIds)) {
$query->whereNotIn($this->manageModel->getQualifiedKeyName(), $existingIds);
}
});
However, removing/disabling this code doesnāt help since October wants to replace the existing record, instead of allowing duplicates.
This can be tested in the test plugin (GitHub - octobercms/test-plugin: Test Suite & Playground for October CMS) by navigating to Users ā Pivot Model tab.
Next inside the following file modules/backend/behaviors/relationcontroller/HasPivotMode.php we can actually see that there was provisions to allow multiple records with the same ID.
// Two methods are used to synchronize the records, the first inserts records in
// bulk but may encounter collisions. The fallback adds records one at a time
// and checks for collisions with existing records.
try {
$this->relationObject->attach($foreignIds, $pivotData);
}
catch (Exception $ex) {
$this->relationObject->sync(array_fill_keys($foreignIds, $pivotData), false);
}
When the attempt to add a second record fails, it simply updates the existing one. Removing that exception handling SQL returns the collision error stopping us (Integrity constraint violation):
"SQLSTATE[23000]:
Integrity constraint violation: 1062
Duplicate entry '2-3' for key 'PRIMARY'
(SQL: insert into `october_test_users_roles` (`clearance_level`, `country_id`, `created_at`, `is_executive`, `role_id`, `salary`, `updated_at`, `user_id`) values (34343334, ?, 2024-05-14 00:20:05, 0, 3, ?, 2024-05-14 00:20:05, 2))
" on line 760 of ~\vendor\laravel\framework\src\Illuminate\Database\Connection.php
Now, we need to remove that primary key constraint from the database, and voila! we have two records with the same ID attached.
A new problem emerges: the pivot data is not unique between the records. Both entries are updated when we update one pivot data (Clearance Level). We need to look into why this happensā¦