inverse-relations and bugs-fixing#111
Open
siggi-k wants to merge 5 commits into
Open
Conversation
139ffcd to
479d22d
Compare
cebe
reviewed
May 22, 2026
| { | ||
| if (!$items->properties) { | ||
| return $this->arbitraryArray(); | ||
| return '[]'; |
Member
There was a problem hiding this comment.
empty object should be {}
Suggested change
| return '[]'; | |
| return '{}'; |
Co-authored-by: Carsten Brandt <mail@cebe.cc>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes and improvements to inverse relation generation in dbmodel.php template
1. Redundant filter logic removed
The method loop was manually rebuilding
$coveredClassesfromrelations,many2manyand
nonDbRelations— the same logic that$inverseRelationsat the top of the templatealready performs. The duplicate code and its variables were removed.
The filter behind
$inverseRelationsalso serves an important purpose beyond deduplication:if the auto-generated inverse relation for a class would be wrong (e.g.
getBookings()insteadof
getBooking()), you can declare the relation manually in the YAML spec usingx-db-type: false.This makes it a
nonDbRelation, which adds the class to$allCoveredClassesand suppressesthe incorrect auto-generated inverse entirely. Instead, an abstract method is generated in the
base class, which the child class then implements correctly.
Example:
Bookinghas a unique FKbank_account_transaction_id. Without a manual declaration,the generator would produce
getBankAccountTransactions()(hasMany) onBooking— which is wrong.By declaring
booking: x-db-type: falsein the YAML, the wrong inverse is suppressed andgetBooking()(hasOne) is implemented manually in the child class instead.2. Broken disambiguation removed (
$i,$number,$usedRelationNames)The old code tried to resolve duplicate method names by appending a number (e.g.
getOrders2()).The counter was incremented on every loop iteration including skipped ones, making the suffix
non-deterministic. This whole mechanism is made obsolete by the new naming approach in point 3.
3. Inverse relation method names now derived from the FK column name
Previously the method name was based on the related class name, which caused naming conflicts
when one model had multiple FK columns pointing to the same target.
Now the FK column name is used as the basis:
_id_<modelName>(the target model's name)Examples: model
Orderhas three FK columns pointing toLead:lead_idgetLead()getOrders()customer_lead_idgetCustomerLead()getCustomerOrders()billing_lead_idgetBillingLead()getBillingOrders()Logic for the inverse name (target model =
Lead, snake =lead):customer_lead_id_id: →customer_lead_lead: →customerCustomer+Orders→getCustomerOrders()If no prefix remains (plain
lead_id→lead→ ``): justgetOrders()FK column names within a model are unique by definition (DB constraint),
so naming conflicts can no longer occur.
4. Missing
@propertyannotations addedThe PHPDoc block at the top of the generated class was missing
@propertyentriesfor inverse relations. IDE type inference for these relations was broken.
5. Bug fix: skip models with
x-table: falseModels declared with
x-table: falsehave no real database table and no actual FK columns.The filter for
$inverseRelationsnow excludes them via$r->getTableName() !== ''.6. Bug fixes and improvements in
fakeForArrayandfakeForObject(FakerStubResolver.php)Bug fixes:
fakeForArray: an array property with noitemsdefinition returned$faker->words()— now returns[]fakeForObject: an object property with nopropertiesdefinition returned$faker->words()— now returns[]fakeForArraywith object items: whenfakeForObjectreturned[], it was still wrapped inarray_map()viawrapInArray(), producing broken faker code — now[]is returned directlyImprovements:
fakeForObjecthas a new$depthparameter enabling correct indentation for nested objects in the generated faker output$this->{__FUNCTION__}($prop)with the explicit$this->fakeForObject($prop, $depth + 1)