在模型中使用cakephp组件 [英] use cakephp component inside a model

查看:179
本文介绍了在模型中使用cakephp组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用我在一个模型类中在cakePHP中创建的组件?



如果是这样,请让我知道我该如何做。

解决方案

不良做法 %93controllerrel =nofollow> MVC 框架。如果您认为需要在模型中使用组件,那么您应该重新考虑并重新组织代码,因为一旦出现错误





要在模型之间共享可重用的代码,这将是一种行为。对于视图,它将是一个帮助器。



如果你有一些真正的通用代码,它应该是一个lib或将其放在Utility文件夹/命名空间或创建一个新的命名空间。



没有提供代码,因此不可能给出任何真正的建议,如何重构它。然而,你想使用现有的代码的方式将不能在MVC上下文中工作,所以时间重新思考你的做法,无论你尝试做什么。


How do I use a component that I created in cakePHP inside one of my model classes? Is this possible?

If so, please let me know how I can do so

解决方案

It is possible but pretty bad practice in a MVC framework. You should re-think and re-organize your code if you think you need to use the component in a model because something is cleary wrong then.

A component is thought to share code between controllers, only between controllers.

To share re-usable code between models it would be a behavior. For a view it would be a helper.

If you have some really generic code it should be a lib or put it in the Utility folder / namespace or create a new namespace. Check the existing classes there to get an idea what to put in there.

No code was provided so it is not possible to give any real recommendation on how to refactor it. However the way you want to use the existing code won't work in the MVC context, so time to rethink your approach of whatever you try to do.

这篇关于在模型中使用cakephp组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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