active-record icon indicating copy to clipboard operation
active-record copied to clipboard

Realize `ReadOnlyActiveRecord`

Open Tigrov opened this issue 1 year ago • 4 comments

There are cases when ActiveRecord instances should be used in readonly mode. For example, readonly tables or views (virtual tables) and views for rendering responses.

For these cases ReadOnlyActiveRecordInterface interface could be created and realized in ReadOnlyActiveRecord class.

Updated 16.05.2024 Found related issue with naming ImmutableActiveRecord

  • https://github.com/yiisoft/yii2/issues/10459

Tigrov avatar May 15 '24 10:05 Tigrov

Can we use native PHP readonly properties (PHP 8.1) and classes (PHP 8.2) for the purpose?

readonly class User
{
    // ...
}

class User
{
    public function __construct(
        public readonly string $name
    )
    {}

    // ...
}

samdark avatar May 15 '24 19:05 samdark

Can we use native PHP readonly properties (PHP 8.1) and classes (PHP 8.2) for the purpose?

Looks like a solution but still can be deleted using $model->delete()

Tigrov avatar May 16 '24 05:05 Tigrov

May be add this feature in PHP 8.2 only? It will to allow check readonly class in delete() method and throw exception in this case.

vjik avatar May 18 '24 17:05 vjik

Seems readonly class is not suitable due to relations stored in $relation property. And the relations can be loaded in several operations.

Tigrov avatar May 21 '24 15:05 Tigrov