twill icon indicating copy to clipboard operation
twill copied to clipboard

Allow Runtime Table Aliassing

Open tylernathanreed opened this issue 10 months ago • 1 comments

Description

One of my packages, reedware/laravel-relation-joins received an issue report (#34) indicating that it was partially incompatible with your project.

Upon further investigation, I saw that the root cause was the getTable method on models being overridden in such a way that setTable is no longer respected.

My package allows you to join on relations by name, and sometimes those joins need to be aliased, like so:

Video::query()->joinRelation('relatableItems as relatable')

When an alias is used in my package, the underlying implementation is to swap out the table name of the model with the aliased counterpart so that any qualified constraints are bound against the aliased table, rather than the original one. This relies on the ability to set a model's table during runtime that doesn't reflect the actual model's table, but rather an alias in a query that's actively being constructed.

However, because your implementation of getTable does not support setTable, the approach in my package falls apart, as it needs to be able to rely on setTable. Thankfully, the fix is quite simple. Functions like this:

public function getTable()
{
    return config('twill.related_table', 'twill_related');
}

Just need to become this:

public function getTable()
{
    return $this->table ?? config('twill.related_table', 'twill_related');
}

This PR updates all overrides of getTable to follow suit, thus resolving the partial incompatibility between our two projects.

tylernathanreed avatar Mar 11 '25 13:03 tylernathanreed

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Mar 11 '25 13:03 CLAassistant