Understanding Yii's Achitecture


I am fairly new to Yii but have experience with other MVC frameworks, such as RoR.

I’m building my first Yii app and am finding myself repeating code numerous times in a view file to check values of my models attributes (e.g. value > 0, value != ‘’, etc) and also formatting these values (mostly with number_format()).

My question concerns where to put this logic in order to "DRY" my code. Should it go in the relevant controller, or the model? I initially thought to put it in the model, maybe in afterFind() and beforeSave() (because I want to strip any formatting from numerical values before they go back into MySQL).

But I’m not sure exactly where to put it - maybe in a behavior or component? I don’t fully understand the purpose of those two elements yet, nor do I get the filter() functionality in Yii yet.

Can anyone point me to a tutorial or other reference that would provide a more detailed explanation of behaviors, components, and filters? Or maybe give me a frame of reference in terms of the corresponding elements in Ruby on Rails? I have read the Definitive Guide and API documentation concerning behaviors, components, and the filtering system, but I think I need to see some working examples to really grasp the potential they have.

Thanks in advance!


AR behavior should fit if you are going to reuse this.

You can get some examples here: http://code.google.com/p/yiiext/

Check ensureNull, status and taggable.

Cool, thank you! I will check those out.

Also see


Thank you both! Good info at both of those links.