CakePHP:在控制器中获取当前模型名称 [英] CakePHP: get current model name in a controller

查看:294
本文介绍了CakePHP:在控制器中获取当前模型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要记录当前模型名称的行为。如何从CakePHP中的控制器中获取当前模型名称?

I'm creating a behaviour that needs to log the current model name. How can I get the current model name from within a controller in CakePHP?

推荐答案

您的架构有缺陷,控制器的主模型名称。这是违反了MVC模式。您的应用程序应该具有胖模型和瘦的控制器。

Your architecture is flawed when you want to get a controllers primary model name from a behavior. That's a violation of the MVC pattern. Your app should have fat models and skinny controllers.

您的行为已经具有模型实例,因为它需要传递给行为。您可以从模型对象中获取名称:

Your Behavior already has the model instance as it is required to be passed to behaviors. You can get the name from the model object:

// Cake 2.x
$Model->name
// Cake 3.x
$Table->name()

或其别名

// Cake 2.x
$Model->alias
// Cake 3.x
$Table->alias()

这篇关于CakePHP:在控制器中获取当前模型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆