PHP-Yii-动态属性CActiveRecord


PHP - Yii - dynamic properties CActiveRecord

我有一个类Person,它具有属性{property}_{countrycode}。此类可以有20个属性,每个属性都有国家/地区代码。20x3=类中定义的60个属性。对于每种语言,我都需要在课堂上手动定义它们。

class Person extends CActiveRecord {
    public $name_sk;
    public $name_cz;     
    public $name_de;
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }
    function tableName() {
        return 'person';
    }
}

问题:如何在类中动态生成/定义这些属性?

示例:

public function __construct() {
    $langs = array('sk', 'cz', 'de');
    $properties = array('name', 'surname', 'age');
    foreach($langs as $lang) {
        foreach ($properties as $k => $value) {
            $this->{$value. "_". $lang} = null;
        }
    }
}

在Yii 1.x中,你不能。仅当您通过扩展CActiveRecord._get()来覆盖它时。