how to get current model class / name?

I’ve gotta be looking right through something. I’m extending CController, and want to dynamically determine what the model class is for the current controller / action. Is there somewhere that the name is stored? Or a standard way to extract it from the web request?

My intention is to find / add an easy way to get a new model object wherever I need it, including in parent classes.

[edit] For example, if the code in an application component (created upon application startup) wants to know what model(s) are in use so it can perform a statistical function on each model, how can it find that out?[/edit]

Thank you kindly. ^_^

Models are independent of Controllers, you can use as many models as you’d like in any 1 controller, are you confusing models with something else?

Maybe controller and action name?

You can always use CLASS inside a class to reference its name in PHP.

In Yii:

In a class you can get the controller id by using $this->id;

If your controller is named SiteController, it will return ‘site’;

You get the full class name CLASS which will return SiteController or use $this->id like this ucfirst($this->id).‘Controller’;

to get the name of the action use $this->action->id. the actual method name would be ‘action’.ucfirst($this->action->id);

Thank you for the reply, I appreciate the help. I got a hint about what I might need to do from your last point about [font="Courier New"]CController::action. [/font]

My goal is/was to create a function that determines which model(s) the controller is currently using. The more I look into it, the more it makes sense that I’ll need to roll my own (simple) functionality for each controller - model relationship, and won’t be able to create a [font=“Courier New”]ControllerEx[/font] class with a method that just handles this like [font=“Courier New”]YiiBase::app()->getController()[/font] handles controllers.

Unless someone has a better idea, I’ll create an abstract function in [font=“Courier New”]ControllerEx[/font] and each of my controllers will be required to implement [font=“Courier New”]getActiveModels()[/font].

I’m a newbie, so am used to what yiic generates, but understand that that’s just one way to relate controllers to models. I had been looking at the generated CRUD controllers, and realized that SiteController doesn’t follow that pattern since its model, LoginForm, doesn’t match the yiic generation pattern, therefore I couldn’t universally use the action / controller prefix to determine the model.

Thanks again!

[edited for rather dramatic typo]

I ran into a similar issue and ended up extending CController. It’s part of my extension library (psYiiExtensions) called CPSController. It’s in SVN but not yet packaged.

on any view or controller , you can get class like


echo $myclass = get_class($model)

or


echo $myclass = get_class($this)

:rolleyes:

I put this in my globals.php file

(BTW: See a very useful blog on that subject here: http://www.yiiframework.com/wiki/31/use-shortcut-functions-to-reduce-typing)


function modelname()

{

	return Yii::app()->controller->id;

}

It seems to work from models, controllers and views, but it is not thoroughly tested.