我有一个类人,它有属性{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;
}
}
}发布于 2015-06-04 15:04:38
在YII1.x你不能。只有当您通过扩展CActiveRecord._get()来覆盖它时。
https://stackoverflow.com/questions/30645749
复制相似问题