Names for model variables

Hi all!

I have just started using Yii, still hovering somewhere between being fascinated and overwhelmed. A rather generic question sprung up on me yesterday:

When building controllers with gii, the default variable name given to a new model instance is $model. I have a feeling that at some point I will want to use content provided via different models at the same time.


  • Wouldn’t there be a conflict resulting from the same name for each model, e.g., when wanting to build a nested table in one view (e.g., customer and products bought)?

  • Would it thus be a recommendable practice to give more specific names to the instance variables (e.g., $customer = new Customer, $product = new Product)?

  • Or would that idea conflict with some pattern whose importance I have not yet realized?

If I am not mistaken, this may in most cases be taken care of by properly defining relations between sets (i.e., tables) of data as well, thus always being able to address data relationships from a single model (e.g., $customer->product_id, given that there would be a join table linking customers and products). Is that correct?

TIA for your ideas


Simple answer: Change/use whatever variable names you prefer instead. $model is just a generic name used in the auto generated example code.

For code re-use I think it’s best to stick with $model but descriptive names have their place too. FWIW I use $model more often than a unique and more meaningful identifier.