开发者

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

开发者 https://www.devze.com 2023-01-29 02:17 出处:网络
in API , call $this->model will return model\'sname but it dont work. http://api13.cake开发者_开发问答php.org/class/view

in API , call $this->model will return model'sname but it dont work.

http://api13.cake开发者_开发问答php.org/class/view

is api cakephp false ? even $view->modelId dont work, too.


In controller:

$this->modelClass


Try this

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

this should change your controller name in to model name. And you can do it of course from view level.


yes you can, you need to tell your model to tell its name. so write a function in your model like:

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

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


Use Inflectors to achive your wanted result:

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


You can't. That's because the view's parent is a controller (which you can't also access), and the controller could have multiple models.

If you are trying to access the Model's name in your View it is very likely you are doing something wrong or simply you haven't understood the MVC design pattern yet.

I can't think of one case which a Model's name is relevant to the view. I insist, you are doing something wrong.


try this one:

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


If you are following the CakePHP Conventions and Rules, a model name is the same as a Controller Name but in singular, the Controller name should be plural, so to get the controller name in View, simple add the following:

<?php
$controller = $this->name

and the model name in view will be:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消