-
Notifications
You must be signed in to change notification settings - Fork 33
Open
Labels
Description
E.g.
$this->belongsToMany('Administratives', ['through' => 'PeopleAdministratives', 'className' => 'People', 'foreignKey' => 'person_id', 'targetForeignKey' => 'administrative_id']);
$this->belongsToMany('AdministrativesClients', ['through' => 'PeopleAdministratives', 'className' => 'People', 'foreignKey' => 'administrative_id', 'targetForeignKey' => 'person_id']);With GET request:
{{protocol}}{{domain}}/api/people/103c3b19-707f-4d3d-861b-000000000000/relationships/administratives
Will result in the relationship data providing the wrong column – i.e. person_id instead of the desired administrative_id
Required edit to JsonApiView.php
- $result = $encoder->encodeIdentifiers($entity->get($association->getProperty()));
+ $relationshipData = $entity->get($association->getProperty());
+
+ $transformedData = [];
+ if ($relationshipData) {
+ foreach ($relationshipData as $item) {
+ if (isset($item->_joinData)) {
+ // Always use the targetForeignKey from the association
+ $targetField = $association->getTargetForeignKey();
+ if (isset($item->_joinData->{$targetField})) {
+ $transformedItem = clone $item;
+ $transformedItem->id = $item->_joinData->{$targetField};
+ $transformedData[] = $transformedItem;
+ }
+ }
+ }
+ }
+
+ $result = $encoder->encodeIdentifiers($transformedData);