Yii Framework Forum: how to get current model class / name? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

how to get current model class / name? Rate Topic: ***** 1 Votes

#1 User is offline   Ivo Roper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 22-September 09
  • Location:Seattle, WA, USA

Posted 24 October 2009 - 12:05 AM

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. ^_^
0

#2 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 24 October 2009 - 09:33 AM

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);
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#3 User is offline   Ivo Roper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 22-September 09
  • Location:Seattle, WA, USA

Posted 24 October 2009 - 03:33 PM

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 CController::action.

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 ControllerEx class with a method that just handles this like YiiBase::app()->getController() handles controllers.

Unless someone has a better idea, I'll create an abstract function in ControllerEx and each of my controllers will be required to implement getActiveModels().

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]

View Postjayrulez, on 24 October 2009 - 09:33 AM, said:

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

<snip>

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

0

#4 User is offline   lucifurious 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 184
  • Joined: 15-March 09
  • Location:Atlanta, GA.

Posted 25 October 2009 - 10:56 AM

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.
0

#5 User is offline   emrald 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 20-July 11

Posted 14 October 2011 - 12:26 AM

on any view or controller , you can get class like

echo $myclass = get_class($model)


or
echo $myclass = get_class($this)

:rolleyes:
1

#6 User is offline   Binc 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 11-January 12

Posted 12 January 2012 - 04:05 AM

I put this in my globals.php file
(BTW: See a very useful blog on that subject here: http://www.yiiframew...o-reduce-typing)

function modelname()
{
	return Yii::app()->controller->id;
}


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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users