justinvoelker
justinvoelker

Reputation: 573

Yii2 model attribute without corresponding field in database

To make a long story short I am building some simple audit functionality that stores a database records' previous values in an auditing table. I use "getOldAttributes" to find the old values of all database columns for the record being audited. For one model, I have another attribute named permissions (does not exist as database column), that I would like to store right along side the other oldAttributes. Adding "public $permissions;" to the top of my ActiveRecord class is working for passing data from the form into the model for processing but that attribute is not included in the getOldAttributs call. I have read various ideas on using getters/setters to make that attribute work like a regular AR attribute but nothing is working or even really making sense.

TLDR; AR class has public attribute that I want to be able to access like all other database attributes (but without actually saving in the database).

Upvotes: 3

Views: 2020

Answers (1)

Alex
Alex

Reputation: 8072

Try to override getAttributes() method of ActiveRecord:

public function getAttributes($names = null, $except = [])
{
    return array_merge(['permissions'], parent::getAttributes($names, $except));
}

Upvotes: 1

Related Questions