Skip to content

Optional relationship keys and typed relation classes - #377

Merged
techmahedy merged 4 commits into
doppar:4.xfrom
techmahedy:techmahedy-4.x
Oct 17, 2026
Merged

techmahedy merged 4 commits into
doppar:4.xfrom
techmahedy:techmahedy-4.x

Conversation

@techmahedy

@techmahedy techmahedy commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Pull Request Checklist

Q A
Branch? 4.x
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Summary

Relationship methods now work with just the related model, and they return their own classes so you can declare a return type.

public function otp(): LinkOne
{
    return $this->linkOne(Otp::class);
}

Before, this was not possible. All the key arguments were required, and the methods returned a plain Builder, so there was no class to put in a return type.

The problems

  1. Every key was mandatory. linkOne, linkMany, bindTo and bindToMany all required their foreign key, local key and pivot table arguments. In almost every app these follow the same naming convention, so users repeated the same strings in every model.
  2. No relation return types. The methods returned Builder with no declared type. Writing : LinkOne failed because the class did not exist, and users could not annotate relationship methods with anything more specific than Builder.

Typed relation classes

Four new classes in Phaseolies\Database\Entity\Relations:

Method Returns
linkOne() LinkOne
linkMany() LinkMany
bindTo() BindTo
bindToMany() BindToMany

Each one extends Builder and adds no behavior. Everything that worked on a relation query (where(), orderBy(), count(), first()) still works. Existing return types of Builder stay valid.

Docs

relationships.md in the docs repo has a new "Relationship Return Types" section. The four relationship examples now use the short form with a return type, and each comes with a table of arguments and defaults. The "all the arguments are mandatory" sentences were removed.

Checklist

  • Tests have been added or updated
  • Documentation has been updated
  • Code follows the project coding standards
  • All tests pass locally

@techmahedy techmahedy added enhancement New feature or request feat new feature labels Oct 17, 2026
@techmahedy techmahedy self-assigned this Oct 17, 2026
@techmahedy
techmahedy merged commit 3d59f24 into doppar:4.x Oct 17, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feat new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant