Add properies dynamically

Is there a way to add some properties dynamically to a model?

Let's say I have a field called "strName" in my model, and this should be "splitted" into "strNameDe" and "strNameEn" for translation. The fields "strNameDe" and "strNameEn" doesn't exist in the model, they are stored into another table.

By now, I've have to declare:

public $strNameDe;

public $strNameEn;


public function rules()


  return array(



    array('strNameDe, strNameEn', 'required'),



I have also to add the following function to my model:

public function safeAttributes()


    return CArray::merge($this->attributeNames(), array('strNameDe', 'strNameEn'));


Otherwise I will get an error, that these two fields doesn't exist in my model.

I want this to have more dynamically, because this might handy for only 1 field, but if I have 5 or more fields, this gets a bit nervy. I'm thinking about how this could be handled by defining an array, containing all the fields, which are multilingual like:

public $arrFieldTranslation = array('strName', 'strTitle', 'strText');

but I don't know how to get the following properties (without setting them by hand):

public $strNameDe;

public $strNameEn;

public $strTitleDe;

public $strTitleEn;

public $strTextDe;

public $strTextEn;

I have a lot of models, with a lot of fields to be mulitlingual, and I urgently need a solution.

I have a behavior which stores and loads the translated values, and everthing works fine by now, but I want to get this stuff more dynamic, because if I want to define a third language, I have to go through all of my models, and make the changes.

Is there a way, to add the properties dynamically, or the tell Yii, not to throw an error if these fields are not declared in the model?

Is your problem only about the trouble declaring those attributes in safeAttributes()? If so, in your safeAttributes(), you could exploit $arrFieldTranslation and see what attributes can be safe.

no, my problem is to declare public members dynamically. i think the rest could be handled automatically. if i don't declare all properties, i get an error. i don't to declare all field with "public $…" because this could be a lot of fields.

You don't have to declare all your properties. Do it as you always code in OOP fashion. It's interesting topic, lets see if this helps:

[color=green]In your model[/color]

1. Declare some properties. One class property as a placeholder for dynamic fields, one array with your preferred field names and one for languages

2. The power of PHP5, magic methods. Now override __get() and __set()

3. Initialize your properties. This method will set all of them by language. It will create an array like this: ‘strTextMk’ = null, ‘strTitleMk’ => null …>

Ok, now we have getter/setter for properties and a nice function to create them.

[color=green]In your controller, or elsewhere[/color]

Also, you can isolate global logic in one model and extend other models from there. Override specific methods as needed. Go OOP ;)

Probably it's not the best solution, but I hope it will help you somehow.