简体   繁体   中英

How can I make private ActiveRecord db attribute in Yii2?

For example I have a model Person that was created from db table 'person' with fields 'firstname' and 'lastname' using Gii model generator. I can access these attributes like

$person = Person::findOne($PersonId);
$personFirstname = $person->firstname;

If i created custom attribute fullname using getters and setters:

public function getFullname()
{
    return $this->firstname .' '.$this->lastname
}

I don't need 'firstname' and 'lastname' anymore. I i would like to hide them. Make private. How can I do that?

You can achieve your goal create new model and extend his from Person . And set property which you need. How it works see this and this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM