Link neteen Controller and Model

Hi all,

as far as I know there is no link between a controller and a model.

In a situation where I have a BaseController for all controllers it can be handy to refer to a so called linked Model (default Model of a controller so to say) so that I can have one method in the BaseController to perform the same operation for the controller's model.

for instance I have a actionDuplicate in the BaseController which is in charge of calling a generic model duplicate method to hum … duplicate a record

I had to declare a model variable for all controllers (in BaseController) and assign the 'ucfirsted' controller->id like this in an init method (overriden from CController)

this suits me fine but as there is no such link (between Controller and Model) by design I was wondering if my approach had hidden falws that I cannot see

or if someone would have another approach

thanks in advance

hope this is of interest to someone

What you did is good if your controllers are very regularized (i.e., one controller corresponding to exactly one model).

One small tip: to get the model instance, you can call:

CActiveRecord::model($className)

instead of using call_user_func().

hi

thanks for the reply

I just found out abut the tip :) was having a look at the validators

would this technique of passing the model to the static method help regarding the other thread on late static binding or am i confusing things ?

This is normal PHP usage. The class name is a string.

Hi Thomas,

Just a quick thought I had while reading this topic, was that I'm not sure how comfortable I would be over riding the init() function.

I wonder if using the beforeAction() function would have the same effect and yet leave init() nice and safe :wink:

Simon