简体   繁体   English

如何在Yii2中设置私有ActiveRecord db属性?

[英]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. 例如,我有一个模型Person,是使用Gii模型生成器从数据库表“ person”创建的,其字段为“ firstname”和“ lastname”。 I can access these attributes like 我可以访问这些属性,例如

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

If i created custom attribute fullname using getters and setters: 如果我使用getter和setter创建自定义属性fullname

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 . 您可以实现自己的目标,创建新模型并从Person扩展他的模型。 And set property which you need. 并设置所需的属性。 How it works see this and this . 它是如何工作看到这个这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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