How to get the name of the current model this view context is attached to?

in the API, calling the $ this-> model will return the model name, but it does not work.

http://api13.cakephp.org/class/view

api cakephp false? even $ view-> modelId doesn't work either.

+4
source share
7 answers

In the controller:

$this->modelClass 
+12
source

try it

 Inflector::classify( $this->params['controller']); 

this should change the name of your controller in the model name. And you can do this, of course, from a presentation level.

+8
source

Yes, you can tell your model to say your name. so write a function in your model, for example:

  function myname(){ return $this->name; } 

and now your controller can request your models for their names.

+3
source

Use Inflectors to achieve the desired result:

 <?php $model = Inflector::camelize(Inflector::singularize($this->params['controller'])); ?> 
+2
source

You can not. This is because the parent view is the controller (which you also cannot access), and the controller can have several models.

If you are trying to access the model name in your view, it is very likely that you are doing something wrong or that you just did not understand the MVC design pattern.

I can not come up with one case that relates to the name of the model. I insist you are doing something wrong.

0
source

try the following:

 $view =& ClassRegistry::getObject('view'); $models = $view->params['models']; 
0
source

If you follow the conventions and rules of CakePHP, the model name matches the name of the controller, but in the singular, the name of the controller must be plural, so to get the name of the controller in the view, just add the following:

 <?php $controller = $this->name 

and the model name will look like this:

 $model = trim($controller , "s"); 

this is the only way to get the model name in sight

-1
source

All Articles