Repository navigation
Conversation
The BelongsTo branch of the LinksHandler joined the child table on "child.pk = <id>", which never matches the parent row: the query returned the first row of the parent table whatever the URI variable was. Join on the foreign key, filter on the child identifier and only select the parent columns (plus the child identifier, read back to build the IRI).
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ee99ab3813
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| ->select($model->qualifyColumn('*')); | ||
|
|
||
| // The child identifier is read back by the IdentifiersExtractor to build the IRI. | ||
| if ($identifierField !== $model->getKeyName()) { |
There was a problem hiding this comment.
Preserve the child identifier when key names collide
When both sides of a BelongsTo link use the common id primary-key name, this condition skips adding the joined child ID, leaving only the target model's id in the result. IdentifiersExtractor::getIdentifierValue() reads the link identifier from that result to build the nested IRI, so if the child and parent IDs differ, the response uses the parent's ID in the path. Preserve the child identifier separately, such as under an alias, rather than treating equal column names as equal values.
Useful? React with 👍 / 👎.
… keys share a name The child identifier was only selected when its column name differed from the key name of the related model. With two models keyed by "id", the related model's own "id" was read back instead, so the IRI of the returned item contained the wrong identifier. The child identifier is now always selected under an alias and read back from it by the IdentifiersExtractor.
7e6f8e2 to
5ff53e2
Compare
A sub-resource that follows a
BelongsTorelation always returns the first row of the parent table, whatever the URI variable is. With the workbench models:#[ApiResource( uriTemplate: '/grand_sons/{id_grand_son}/grand_father', uriVariables: ['id_grand_son' => new Link(fromClass: GrandSon::class, fromProperty: 'grandfather', identifiers: ['id_grand_son'])], operations: [new Get()], )] class GrandFather extends Model {}GET /api/grand_sons/3/grand_father, where grandson 3 belongs to grandfather 3:"idGrandFather": 1,"sons": ["/api/grand_sons/1"])"idGrandFather": 3,"sons": ["/api/grand_sons/3"])The
LinksHandlerbuiltjoin(child_table, child.pk, <id>), so the ON clause compared the child key to a quoted identifier and never involved the parent table. In practice that is a cross join, andfirst()picks the first parent. Because ofselect *, the child's columns (name,created_at...) also overwrote the parent's. The existingtestBelongsTocreates a single record, so it could not catch this. On MySQL the quoted identifier becomes an unknown column, so I expect an error there rather than a wrong row.The fix joins on the foreign key, filters on the child identifier and selects only the parent columns. The child identifier is still selected when it is not the parent key, because the
IdentifiersExtractorreads it back to build the IRI (/api/grand_sons/3/grand_father). Covered bytestBelongsToReturnsTheRelatedIteminsrc/Laravel/Tests/EloquentTest.php.