PHPackages                             rasuvaeff/yii3-tenancy-db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. rasuvaeff/yii3-tenancy-db

ActiveLibrary[Database &amp; ORM](/categories/database)

rasuvaeff/yii3-tenancy-db
=========================

Database tenant storage for rasuvaeff/yii3-tenancy

v2.0.2(2w ago)00BSD-3-ClausePHPPHP 8.3 - 8.5CI passing

Since Jul 4Pushed 1mo agoCompare

[ Source](https://github.com/rasuvaeff/yii3-tenancy-db)[ Packagist](https://packagist.org/packages/rasuvaeff/yii3-tenancy-db)[ Docs](https://github.com/rasuvaeff/yii3-tenancy-db)[ RSS](/packages/rasuvaeff-yii3-tenancy-db/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (35)Versions (5)Used By (0)

rasuvaeff/yii3-tenancy-db
=========================

[](#rasuvaeffyii3-tenancy-db)

[![Stable Version](https://camo.githubusercontent.com/42414a7f27550541802a7b33674acc89da638b7a5ceecd505eb440669b97bab1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7261737576616566662f796969332d74656e616e63792d64623f6c6162656c3d737461626c6526736f72745f73656d7665723d31)](https://packagist.org/packages/rasuvaeff/yii3-tenancy-db)[![Total Downloads](https://camo.githubusercontent.com/4c8820de31be8fb80ae51cb0d54b06c130c28f8901e32b3b5c3a383c1cb643ab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7261737576616566662f796969332d74656e616e63792d6462)](https://packagist.org/packages/rasuvaeff/yii3-tenancy-db)[![Build](https://camo.githubusercontent.com/46f4611a6c349602a04700521cef0cf64dd07c96137db9d9fc40b15736314b0a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d74656e616e63792d64622f6275696c642e796d6c3f6272616e63683d6d6173746572)](https://github.com/rasuvaeff/yii3-tenancy-db/actions)[![Static analysis](https://camo.githubusercontent.com/d48dc09df68c01a611721305cc334c2b1ada0ba4183b602ea67d4cd6803fdd50/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d74656e616e63792d64622f7374617469632d616e616c797369732e796d6c3f6272616e63683d6d6173746572266c6162656c3d737461746963253230616e616c79736973)](https://github.com/rasuvaeff/yii3-tenancy-db/actions)[![Psalm level](https://camo.githubusercontent.com/96f0376e917393dc04f5e50ef34fa74f202272a3100aa70c66ece3282f6a2e36/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7073616c6d2d6c6576656c253230312d3134314634383f6c6f676f3d7073616c6d266c6f676f436f6c6f723d7768697465)](https://github.com/rasuvaeff/yii3-tenancy-db/blob/master/psalm.xml)[![PHP](https://camo.githubusercontent.com/3e2157f6c4cb9382e8eebc0efafacc6ca08f51d864271a8b7c223436236ff9cc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7261737576616566662f796969332d74656e616e63792d64622f706870)](https://packagist.org/packages/rasuvaeff/yii3-tenancy-db)[![License](https://camo.githubusercontent.com/bacbf7b5bb4d8df8c7a2f53ebf6200c34af3d776fccf617c42cf8d262fa2265b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7261737576616566662f796969332d74656e616e63792d6462)](LICENSE.md)

Database tenant storage for [rasuvaeff/yii3-tenancy](https://github.com/rasuvaeff/yii3-tenancy): `TenantProvider` backed by a `tenants` table via yiisoft/db, an optional PSR-16 read-through cache, and a ready-made migration.

> **Using an AI coding assistant?** [llms.txt](llms.txt) contains a compact API reference you can share with the model. Contributors: see [AGENTS.md](AGENTS.md).

Requirements
------------

[](#requirements)

RequirementVersionPHP8.3 – 8.5`rasuvaeff/yii3-tenancy``^1.0``yiisoft/db``^2.0``yiisoft/db-migration``^2.0` (for the bundled migration)Installation
------------

[](#installation)

```
composer require rasuvaeff/yii3-tenancy-db
```

Register the migrations path and run the migration:

```
// config/params.php
'yiisoft/db-migration' => [
    'sourcePaths' => [dirname(__DIR__) . '/vendor/rasuvaeff/yii3-tenancy-db/migrations'],
],
```

```
./yii migrate:up
```

Usage
-----

[](#usage)

With `yiisoft/config` no wiring is needed — this package binds `TenantProvider` to `DbTenantProvider` (the core deliberately leaves that interface unbound; installing core + this backend just works):

```
use Rasuvaeff\Yii3Tenancy\CurrentTenant;

final readonly class InvoiceService
{
    public function __construct(private CurrentTenant $currentTenant) {}
    // TenantResolutionMiddleware looks tenants up through DbTenantProvider
}
```

Manual construction:

```
use Rasuvaeff\Yii3TenancyDb\CachedTenantProvider;
use Rasuvaeff\Yii3TenancyDb\DbTenantProvider;

$provider = new DbTenantProvider(db: $connection, table: 'tenants');

// optional PSR-16 read-through cache
$cached = new CachedTenantProvider(inner: $provider, cache: $psr16, ttl: 60);
$cached->forget('acme');   // drop the entry after updating/suspending a tenant
```

Caching semantics: only **found** tenants are cached (a newly created tenant appears immediately); cache read/write failures are non-fatal; entries expire by TTL or explicit `forget()`.

Enable the cache through params:

```
// config/params.php
return [
    'rasuvaeff/yii3-tenancy-db' => [
        'table' => 'tenants',
        'cache' => ['enabled' => true, 'ttl' => 60],
    ],
];
```

Table schema
------------

[](#table-schema)

ColumnTypeNotes`id``string(64)` PKmust satisfy core `Tenant::isValidId()``name``string(190)`default `''``status``string(20)``active` (default) / `suspended``attributes``text`JSON object, default `'{}'`Invalid rows (unknown status, malformed JSON, invalid id) throw `InvalidTenantRowException` — never silently skipped or defaulted.

Components
----------

[](#components)

ClassRole`DbTenantProvider``TenantProvider` over yiisoft/db: single-row `find()` by primary key`CachedTenantProvider`PSR-16 read-through decorator (`yii3-tenancy-db.tenant.{key}`), `forget()` invalidation`Exception\InvalidTenantRowException`thrown by the internal row mapper on invalid rowsSecurity
--------

[](#security)

- Lookups use bound parameters via the yiisoft/db query builder — no SQL string interpolation.
- The table name is configuration (developer-controlled), not user input.
- Rows are strictly validated on read; a corrupted row fails loudly instead of producing a half-valid tenant.

Examples
--------

[](#examples)

See [examples/](examples/) for a runnable script.

ScriptShowsNeeds server?[`db-provider.php`](examples/db-provider.php)Migration + lookup + cached lookup on in-memory SQLitenoDevelopment
-----------

[](#development)

No PHP/Composer on the host — run in Docker via the `composer:2` image:

```
docker run --rm -v "$PWD":/app -w /app composer:2 composer build
```

Or with Make: `make build`, `make cs-fix`, `make psalm`, `make test`.

License
-------

[](#license)

BSD-3-Clause. See [LICENSE.md](LICENSE.md).

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance93

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Total

4

Last Release

18d ago

Major Versions

v1.0.0 → v2.0.02026-07-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0812d5572a7041dfe36e222d295b2e6dc55833a605350fcde58a51a5965ed30?d=identicon)[rasuvaeff](/maintainers/rasuvaeff)

---

Top Contributors

[![rasuvaeff](https://avatars.githubusercontent.com/u/1352718?v=4)](https://github.com/rasuvaeff "rasuvaeff (2 commits)")

---

Tags

databasemulti-tenancyphptenancytenantyii3yiisoft-dbdatabasetenantmulti-tenancyyii3tenancyyiisoft-db

###  Code Quality

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rasuvaeff-yii3-tenancy-db/health.svg)

```
[![Health](https://phpackages.com/badges/rasuvaeff-yii3-tenancy-db/health.svg)](https://phpackages.com/packages/rasuvaeff-yii3-tenancy-db)
```

###  Alternatives

[kreait/firebase-bundle

Symfony Bundle for the Firebase Admin SDK

1555.2M2](/packages/kreait-firebase-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
