Keep specific model functions in a separate file yii.

I have a model which has some functions that are used by my console applications. I want to keep these methods in a separate file. Whats the best way to do that.

You can subclass the model and create a WebModel and a ConsoleModel .

But, while there are no hard rules in programming, in most cases model should only contain logic that doesn’t depend on the context it called from - that’s why you created the model in the first place.

I don’t want to create models for each of the application. Both the applications will share the same model. But i want to keep those functions separate. What about behaviours. Is that works for this kind of scenarios?

@phtamas idea is right way.

If you looking for another solution then might be you can put all your functions in components protected/components directory


class Functions{


   pubic function somefunction($id){

     // your logic goes here.

   }

}

Call it as




$obj = new Functions();

$obj->somefunction($id);

I think this is not good approach. You are not creating new models for each application, you are extending existing models with new one which is the power of OOP. By extending base model class, y[size=2]ou separate programming logic for different applications. This way your code is organized well, and mentainance is much more easier.[/size]

You can define new attribute rules for ConsoleApplication and other application too and much more.