Models and business logic, where ?


I’m new to Yii , i’m Zend Framework user since 5 years.

And i’m learning and reading a lot about Yii that is really great !

I have a question regarding the model and especially the business logic.

I’m an adept to the small controller fat model pratice, as i understand how the models are set using the ORM/Active record pattern is that it is all about DATA, that’s ok we need this but what about the business logic ?

The way i see business logic is "my model has data " and not " my model is data " , so i used to have my models and some data provider ( MYSQL, ORM, API, JSON, TEXT , whatever ) , my Models are agnostic about the DATA come from.

So according to your experience what is the best to design this ?

where and how should i create my Models to use this datas ?

And last question what is hte best way to share this models accross applications or modules ?

Do you have any examples , recommendations , tips about that ?

Thanks .

just follow your way: small controller fat model :) and put business logic in models.

models in Yii are not only DB-oriented (ActiveRecord is special case of models). there are also CFormModel or you can extend CModel on your own. It is not so much different from what you used to know from Zend.

OK i see, thanks for the response .

CModel looks good.

According to your experiences what is the best way to have all models in one place to be shared accross different applications and/or modules ?

you have to choose one directory (it can be /protected/models of one of those application or completly different - your choise). then in config file (main.php and probably other for testing etc) you add:

// firs define alias so you can point to the common directory easily

Yii::setPathOfAlias( 'common', '/opt/common/' );

return CMap::mergeArray( $main, array(

    'import' => array(



        'common.*',  //here you add shared directory to the default search path

        'common.widgets.*', //you can add subdirectories of directory pointed by 'common' alias






then you can access models just by their name (default search path), or if you like by alias: common.ModelName (like in widget function in controller: $controller->widget( ‘common.widgets.MyCommonWidget’ ); )