hipsterjazzbo / Landlord

A simple, single database multi-tenancy solution for Laravel 5.2+
MIT License
615 stars 138 forks source link

Tenants getting mixed up #85

Open aluferraz opened 6 years ago

aluferraz commented 6 years ago

Hi,

After upgrading from Lumen 5.4 to 5.5 and Landlord from 2.0.6 to 2.0.8, my tenants are getting mixed up.

This is the piece of code where a wrong value is added to the "where":

TenantManager:143

  $this->modelTenants($model)->each(function ($id, $tenant) use ($model) {
            $model->addGlobalScope($tenant, function (Builder $builder) use ($tenant, $id, $model) {
                if($this->getTenants()->first() && $this->getTenants()->first() != $id){
                    $id = $this->getTenants()->first();
                }

                $builder->where($model->getQualifiedTenant($tenant), '=', $id);
            });
        });

As you can see in the attached picture, the tenant value for the tenant id is 1. However, the library is setting the value of the tenant to "DS", which is the value of another tenant belongsToSolution.

2017-11-21_15-50-53

The problem seems to be that the library is always getting the first tenant as the value, instead of getting the tenant with the corresponding key. I think the correct code would be:

$this->modelTenants($model)->each(function ($id, $tenant) use ($model) {
            $model->addGlobalScope($tenant, function (Builder $builder) use ($tenant, $id, $model) {        
        $id = $this->getTenantId($tenant);

                $builder->where($model->getQualifiedTenant($tenant), '=', $id);
            });
        });

Lumen: 5.5 LandLord: 2.0.8 PHP: 7.1.9 Enviroment: WAMP Stack (Windows 10) and LAMP Stack (Ubuntu 16)

aluferraz commented 6 years ago

@HipsterJazzbo please, don't give up on us :'(