Skip to content

Fix table prefix duplication in multi-level model inheritance#2353

Merged
glennjacobs merged 1 commit intolunarphp:1.xfrom
somegooser:fix/table-prefix-duplication
Dec 8, 2025
Merged

Fix table prefix duplication in multi-level model inheritance#2353
glennjacobs merged 1 commit intolunarphp:1.xfrom
somegooser:fix/table-prefix-duplication

Conversation

@somegooser
Copy link
Contributor

@somegooser somegooser commented Nov 20, 2025

When extending a model multiple times (e.g., CustomProduct extends Product extends BaseModel), the table prefix was being applied multiple times, resulting in table names like "lunar_lunar_products" instead of "lunar_products".

Problem:
The HasModelExtending::getTable() method instantiated parent classes recursively with (new $parentClass)->table, causing the BaseModel constructor to apply the prefix at each inheritance level.

Solution:

  • Added resolveRootModelClass() to find the root Lunar model in the inheritance chain
  • Added resolveBaseTableName() to get the base table name using reflection without triggering the constructor
  • Modified getTable() to resolve the root model and apply the prefix only once

Test Coverage:
Added test case for multi-level inheritance to ensure prefix is not duplicated across multiple levels of model extension.

When extending a model multiple times (e.g., CustomProduct extends Product extends BaseModel), the table prefix was being applied multiple times, resulting in table names like "lunar_lunar_products" instead of "lunar_products".

**Problem:**
The `HasModelExtending::getTable()` method instantiated parent classes recursively with `(new $parentClass)->table`, causing the BaseModel constructor to apply the prefix at each inheritance level.

**Solution:**
- Added `resolveRootModelClass()` to find the root Lunar model in the inheritance chain
- Added `resolveBaseTableName()` to get the base table name using reflection without triggering the constructor
- Modified `getTable()` to resolve the root model and apply the prefix only once

**Test Coverage:**
Added test case for multi-level inheritance to ensure prefix is not duplicated across multiple levels of model extension.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where table prefixes were duplicated in multi-level model inheritance scenarios (e.g., lunar_lunar_products instead of lunar_products). The issue occurred because each inheritance level triggered the BaseModel constructor, reapplying the prefix. The solution uses reflection to traverse the inheritance chain to the root Lunar model and applies the prefix only once.

Key changes:

  • Refactored getTable() to resolve the root model class and apply prefix once
  • Added helper methods to traverse inheritance chain and extract base table name without triggering constructors
  • Added test coverage for multi-level inheritance scenarios

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
packages/core/src/Base/Traits/HasModelExtending.php Refactored getTable() method and added resolveRootModelClass() and resolveBaseTableName() helper methods to prevent prefix duplication
tests/core/Stubs/Models/Custom/DeepCustomProduct.php Added stub model for testing three-level inheritance chain
tests/core/Unit/Base/Traits/HasModelExtendingTest.php Added test case to verify correct table naming across multiple inheritance levels

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@glennjacobs glennjacobs merged commit a5c4b86 into lunarphp:1.x Dec 8, 2025
45 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Roadmap Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants