rasuvaeff / yii3-filestorage-attachments
Tenant-scoped polymorphic file attachments for rasuvaeff/yii3-filestorage
Package info
github.com/rasuvaeff/yii3-filestorage-attachments
pkg:composer/rasuvaeff/yii3-filestorage-attachments
Requires
- php: 8.3 - 8.5
- rasuvaeff/yii3-filestorage: ^0.10
- rasuvaeff/yii3-filestorage-db: ^0.4
- yiisoft/db: ^2.0
- yiisoft/db-migration: ^2.1.1
Requires (Dev)
- ergebnis/composer-normalize: ^2.51
- friendsofphp/php-cs-fixer: ^3.95
- infection/infection: ^0.35
- maglnet/composer-require-checker: ^4.17
- nyholm/psr7: ^1.8
- rasuvaeff/property-testing-testo: ^1.0
- rasuvaeff/rector-named-literals: ^1.0
- rector/rector: ^2.4
- roave/backward-compatibility-check: ^8.0
- testo/bridge-infection: ^0.1.6
- testo/testo: ^0.10.25
- vimeo/psalm: ^7.0
- yiisoft/cache: ^3.2
- yiisoft/db-mysql: ^2.0
- yiisoft/db-pgsql: ^2.0
- yiisoft/db-sqlite: ^2.0
- yiisoft/di: ^1.4
- yiisoft/injector: ^1.2
- yiisoft/test-support: ^3.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
rasuvaeff/yii3-filestorage-attachments provides a small database-backed
owner-to-file link service for Yii3 Filestorage. It covers the common
polymorphic attachment case without requiring Active Record.
Install
composer require rasuvaeff/yii3-filestorage-attachments
The package requires rasuvaeff/yii3-filestorage ^0.10 and
rasuvaeff/yii3-filestorage-db ^0.4. Run the -db migrations first, then
register this package's migration namespace:
MigrationService::class => [
'setSourceNamespaces()' => [[
'Rasuvaeff\\Yii3FilestorageDb\\Migration',
'Rasuvaeff\\Yii3FilestorageAttachments\\Migration',
]],
],
The config plugin supplies AttachmentTableName and Attachments. It does not
bind FileScopeProviderInterface; applications with tenants bind that contract
once, and the same provider is used by yii3-filestorage-db.
Usage
$attachments->attach( file: $file, ownerType: 'order', ownerId: (string) $orderId, role: 'invoice', ); $invoices = $attachments->forOwner( ownerType: 'order', ownerId: (string) $orderId, role: 'invoice', ); $attachments->detach( file: $file, ownerType: 'order', ownerId: (string) $orderId, role: 'invoice', );
attach() is idempotent and returns false when the exact link already
exists. forOwner() returns list<Attachment> in insertion order; omit
role to list every role. detachOwner() removes every role and file link for
an owner and must be called by the application or an Active Record lifecycle
when that owner is deleted.
"Exactly one invoice per order" is sync() — replacing a role's file set in
one transaction, so a crash cannot leave two invoices where one was meant:
$result = $attachments->sync( files: [$newInvoice], ownerType: 'order', ownerId: (string) $orderId, role: 'invoice', ); // ['added' => 1, 'removed' => 1]
Links whose file is not in the list are removed, missing ones added — both inside one database transaction. Surviving links keep their insertion order (the list's order is not a sort order; there is deliberately no sort column), and every file must be visible in the current scope before anything is written.
Concurrency: the transaction's delete locks the role's rows until commit, so syncs of a role that already holds anything serialize correctly. Two concurrent syncs against a role that is empty on both sides may both commit their sets (the unique key still prevents duplicate links) — serialize such callers if that case matters.
The table has a real foreign key to filestorage_file, so deleting a file row
automatically removes its links. There is deliberately no generic foreign key
to owner tables.
SQLite prerequisite: SQLite enforces
ON DELETE CASCADEonly when every connection enablesPRAGMA foreign_keys = ON. The package does not configure connections injected into it — do this in your application's connection setup. PostgreSQL enforces the cascade by default.
MySQL: it ignores the column-level
REFERENCESof the table migration, so the constraint comes from the follow-up migrationM261127000000AddFilestorageAttachmentFileForeignKey. Before adding it, that migration deletes links whose file no longer exists — on an installation that ran the table migration earlier, every file removed since left its links behind. The migration output prints how many rows it deleted (Deleted N rows); back up the table first if you need them. It cannot be atomic: MySQL commits theDELETEbefore theALTER TABLE, so if adding the constraint fails, the deleted links stay deleted. On SQLite, PostgreSQL and MariaDB 10.5+ — which creates the foreign key from the column clause — it finds the constraint and does nothing.
MySQL/MariaDB compare case-insensitively by default. Owners
aBandAbwere one owner there:forOwner()listed, anddetachOwner()removed, the other's links.M261128000000MakeFilestorageAttachmentKeysCaseSensitive(0.6.0) switchesscope_id,owner_type,owner_idandroleto a binary NO PAD collation —utf8mb4_0900_binon MySQL 8.0.17+,utf8mb4_nopad_binon MariaDB — so case, accents and trailing spaces count: owners42and42are two owners (utf8mb4_binis PAD SPACE and would make them one). MySQL older than 8.0.17 has no such collation and the migration throws there. Runmigrate:upafter upgrading; it rebuilds the table and blocks writes to it meanwhile (seeUPGRADE.md). It does nothing on SQLite and PostgreSQL, which already compare exactly.
Table names and scope
Set rasuvaeff/yii3-filestorage-attachments.attachmentTable and tablePrefix
in application params when the defaults conflict. The migration and runtime
service resolve the same typed AttachmentTableName.
With no scope provider, links use an internal empty scope key. With a bound
FileScopeProviderInterface, every operation is restricted to its current
scope and a file must be visible in that scope before it can be attached. A
bound provider that returns null is a tenant that did not resolve: every
method throws yii3-filestorage-db's MissingScopeException rather than fall
back to the empty key (before 0.6.0 it did, and the file lookup ran without a
tenant predicate). forOwner() reads the owner's files in one scoped query
(DbRepository::findMany()), not one per link.
Verification
make build make test-integration make rector
The integration suite applies the real yii3-filestorage-db and attachments
migrations on SQLite and exercises the documented migration path. The same
suite runs against MySQL or PostgreSQL when FILESTORAGE_TEST_DB names one (CI
does both); the image needs pdo_mysql/pdo_pgsql, which composer:2 lacks:
docker run --rm --network host -v "$PWD":/app -w /app \ -e FILESTORAGE_TEST_DB=mysql -e FILESTORAGE_TEST_MYSQL_PORT=3306 \ <php-image-with-pdo_mysql-and-pdo_pgsql> vendor/bin/testo --suite=Integration