archtechx / tenancy

Automatic multi-tenancy for Laravel. No code changes needed.
https://tenancyforlaravel.com
MIT License
3.67k stars 432 forks source link

Changing the guarded array in the tenant model breaks the data virtual column #1236

Closed zachweix closed 4 months ago

zachweix commented 4 months ago

Bug description

I tried creating a new tenant and I couldn't specify the db name if the guarded array was changed on my tenant model

Steps to reproduce

Create a custom tenant model Use incrementing ids in the database and set id_generator to null in the config file run App\Models\Tenant::create(['tenancy_db_name' => 'acme']); Note that the tenancy_db_name is saved in the database as "acme" Add protected $guarded = ['id']; to the tenant model run App\Models\Tenant::create(['tenancy_db_name' => 'acme2']); Note that the tenancy_db_name is not actually set when saving and therefore the tenancy_db_name is created by default

On a separate note, in the first scenario I also noticed that created_at and updated are saved in the data column, not their own columns until I added them to the getCustomColumns functions, though I think that's a separate bug

Expected behavior

The second database created should have been called acme2

Laravel version

11.15.0

stancl/tenancy version

3.8.4

stancl commented 4 months ago

This is how the virtualcolumn package works, if you think it should be changed you can send a PR there.

zachweix commented 4 months ago

If possible, can you clarify? When I say that id is guarded, why should that change if a column gets added to the data column?

stancl commented 4 months ago

At the moment, $guarded simply needs to be an empty array, the package expects taht. If you'd want to add support for guarded columns, see the virtualcolumn repo and submit a feature request or a PR there.