如何获取此视图上下文附加到的当前模型的名称?

发布于 2024-10-05 07:22:16 字数 219 浏览 3 评论 0原文

在 API 中,调用 $this->model 将返回模型的名称,但它不起作用。

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

api cakephp 是 false 吗?甚至 $view->modelId 也不起作用。

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

少跟Wǒ拽 2024-10-12 07:22:16

在控制器中:

$this->modelClass

In controller:

$this->modelClass
丶情人眼里出诗心の 2024-10-12 07:22:16

试试这个

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

,这应该将您的控制器名称更改为型号名称。当然,您可以从视图级别执行此操作。

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.

黯然 2024-10-12 07:22:16

是的,你可以,你需要告诉你的模型说出它的名字。因此,在您的模型中编写一个函数,例如:

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

现在您的控制器可以向您的模型询问其名称。

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.

三五鸿雁 2024-10-12 07:22:16

使用 Inflectors 来实现您想要的结果:

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

Use Inflectors to achive your wanted result:

<?php $model = Inflector::camelize(Inflector::singularize($this->params['controller'])); ?>
任谁 2024-10-12 07:22:16

你不能。这是因为视图的父级是控制器(您也无法访问它),并且控制器可以有多个模型。

如果您尝试在视图中访问模型的名称,那么您很可能做错了什么,或者只是您还没有理解 MVC 设计模式。

我想不出模型的名称与视图相关的一种情况。我坚持认为,你做错了什么。

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.

聊慰 2024-10-12 07:22:16

试试这个:

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

try this one:

$view =& ClassRegistry::getObject('view');
$models = $view->params['models'];
花期渐远 2024-10-12 07:22:16

如果您遵循 CakePHP 约定和规则,模型名称与控制器名称相同,但为单数,控制器名称应为复数,因此要在视图中获取控制器名称,只需添加以下内容:

<?php
$controller = $this->name

以及模型名称视图将是:

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

这是在视图中获取模型名称的唯一方法

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文