如何获取此视图上下文附加到的当前模型的名称?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在控制器中:
In controller:
试试这个
,这应该将您的控制器名称更改为型号名称。当然,您可以从视图级别执行此操作。
Try this
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:
and now your controller can ask your models for their names.
使用 Inflectors 来实现您想要的结果:
Use Inflectors to achive your wanted result:
你不能。这是因为视图的父级是控制器(您也无法访问它),并且控制器可以有多个模型。
如果您尝试在视图中访问模型的名称,那么您很可能做错了什么,或者只是您还没有理解 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.
试试这个:
try this one:
如果您遵循 CakePHP 约定和规则,模型名称与控制器名称相同,但为单数,控制器名称应为复数,因此要在视图中获取控制器名称,只需添加以下内容:
以及模型名称视图将是:
这是在视图中获取模型名称的唯一方法
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:
and the model name in view will be:
this is the only way to get the model name in view