本文介绍了为什么$ use在cakePHP中被认为是不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表格,其中包含用户信息,一个用于学生,一个用于教师,一个用于管理员。

I have 3 tables that contain user information, one for students, one for teachers and one for administrators.

他们不以任何方式相关。我无法为管理员创建一个仪表板,其中列出了学生和教师的列表。

They are not related in any way. I wan't to create a dashboard for the Administrators, where a list of students and teachers shows up.

我发现实现这一点的唯一方法是使用 $ uses 变量。

The only way I found to achieve this was using the $uses variable in the Administrators controller. However, I have read in many places that this is bad practice.

任何解决方案?

推荐答案

另一个也许更好的做法是使用 ClassRegistry :: init('MyModel') - > myMethod()()

Another, perhaps better practice is the use of ClassRegistry::init('MyModel')->myMethod() (more reading @ Cake API)

这只会载入对象,而不是 loadModel 使用 ClassRegistry 这些模型被视为单例。

This only loads the object when it's used, as opposed to loadModel or uses, with ClassRegistry the models are treated as singletons.

-

有很多不同的模型,您需要从一个控制器访问您的模型数据的全部条件,但永远不会得到如何在不违反约定的情况下如何做的最终答案。

There are plenty of conditions where you would need to access all of your models data, from one controller, but never a definitive answer on how to do it without breaking convention!

这篇关于为什么$ use在cakePHP中被认为是不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:08