Skip to content

Conversation

@abnegate
Copy link
Member

@abnegate abnegate commented Jul 22, 2025

Summary by CodeRabbit

  • Tests

    • Expanded relationship attribute selection tests to cover more scenarios, including additional attributes and bidirectional relationship checks.
  • Refactor

    • Improved internal handling of nested relationship selections for queries, enhancing code clarity and maintainability without affecting user-facing features.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 22, 2025

Walkthrough

The changes refactor the handling of nested relationship selections in database queries by introducing a new private method to process relationship queries, replacing duplicated logic in two methods. Method visibility is adjusted for type checking, and related end-to-end tests are expanded with new scenarios and assertions for relationship attribute selection.

Changes

File(s) Change Summary
src/Database/Database.php Refactored nested relationship selection logic into a new private method; changed method visibility and signature.
tests/e2e/Adapter/Scopes/RelationshipTests.php Extended test to cover more relationship attribute selection scenarios and added new assertions.

Estimated code review effort

2 (~18 minutes)

Poem

In the warren where queries hop and play,
Refactored paths now guide the way.
Relationships nested, selections anew,
Tests expanded, checking what's true.
With every join and every key,
The code grows clearer—oh, what glee!
🐇✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7e8a0c1 and f72e855.

📒 Files selected for processing (1)
  • src/Database/Database.php (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Database/Database.php
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/e2e/Adapter/Scopes/RelationshipTests.php (1)

1152-1212: Comprehensive bidirectional relationship testing - excellent coverage!

This is a thorough test suite that validates relationship attribute selection in both directions. The test scenarios cover various combinations effectively.

Consider these improvements for robustness:

  1. Avoid assuming result ordering: Tests assume model[0] is always 'Fiesta', but database results may not be consistently ordered.

  2. System attributes behavior: Some tests assert system attributes ($id, $permissions, etc.) are present without explicitly selecting them, which seems to be expected behavior but could be documented.

To improve test reliability, consider sorting results or using more specific queries:

// Instead of assuming model[0] is 'Fiesta'
$model = $database->findOne('model', [
    Query::select(['name', 'make.name']),
+   Query::equal('name', 'Fiesta')
]);

Also, consider adding a comment explaining why system attributes are automatically included even when not explicitly selected.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ad7613a and 35e4208.

📒 Files selected for processing (2)
  • src/Database/Database.php (4 hunks)
  • tests/e2e/Adapter/Scopes/RelationshipTests.php (3 hunks)
🧬 Code Graph Analysis (1)
src/Database/Database.php (3)
src/Database/Query.php (6)
  • getMethod (112-115)
  • Query (8-730)
  • getValues (128-131)
  • select (463-466)
  • getAttribute (120-123)
  • setValues (174-179)
src/Database/Exception/Query.php (1)
  • Query (7-9)
src/Database/Document.php (1)
  • getAttribute (212-219)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/Database/Database.php (3)
src/Database/Query.php (6)
  • getMethod (112-115)
  • Query (8-730)
  • getValues (128-131)
  • select (463-466)
  • getAttribute (120-123)
  • setValues (174-179)
src/Database/Exception/Query.php (1)
  • Query (7-9)
src/Database/Document.php (1)
  • getAttribute (212-219)
🔇 Additional comments (7)
src/Database/Database.php (4)

3224-3224: LGTM: Good refactoring to extract duplicated logic.

The extraction of relationship query processing into a dedicated method improves code maintainability and reduces duplication between getDocument and find methods.


6028-6028: Consistent refactoring application.

Good to see the same refactoring pattern applied consistently in the find method, maintaining code uniformity.


6710-6710: Appropriate method visibility and type declaration improvements.

The change from public to private visibility with explicit void return type is good for encapsulation and type safety, assuming this method is only used internally within the Database class.


6723-6786: Well-structured relationship query processing method.

The new processRelationshipQueries method effectively extracts and consolidates the relationship selection logic. The implementation correctly:

  • Handles dot-notated selections by shifting path segments
  • Processes different relationship types (many-to-many, one-to-many, many-to-one, one-to-one) with appropriate logic
  • Maintains proper array indexing with array_values() after unsetting elements
  • Uses clear variable names and follows existing code patterns

The string manipulation for path processing (implode('.', array_slice(explode('.', $value), 1))) correctly converts "foo.bar.baz" to "bar.baz" for nested selection handling.

tests/e2e/Adapter/Scopes/RelationshipTests.php (3)

934-934: LGTM!

The addition of the origin attribute follows the established pattern and supports the expanded test scenarios for relationship attribute selection.


942-944: LGTM!

Converting to a two-way relationship with explicit keys is necessary for the bidirectional relationship testing being added. The configuration correctly establishes the parent-child relationship structure.


953-953: LGTM!

Adding the origin value provides the necessary test data for the new attribute and supports the expanded test scenarios.

if ($query->getMethod() == Query::TYPE_SELECT) {
$values = $query->getValues();
foreach ($values as $valueIndex => $value) {
if (\str_contains($value, '.')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a legit attribute name with DOT.
Can you move this check inside foreach ($relationships as $relationship) ?
So we know it must be a relationship attribute?

@abnegate abnegate merged commit fd24f36 into main Jul 22, 2025
15 checks passed
@abnegate abnegate deleted the fix-relationship-select branch July 22, 2025 07:21
@coderabbitai coderabbitai bot mentioned this pull request Sep 7, 2025
@coderabbitai coderabbitai bot mentioned this pull request Nov 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants