我可以在 CakePHP 的行为中使用特定模型吗?

发布于 2024-09-03 08:33:14 字数 511 浏览 9 评论 0原文

我正在尝试编写一种行为,使我的模型能够访问我设计的简单工作流引擎。工作流引擎本身作为 CakePHP 模型工作,工作流数据存储在数据库中,就像存储任何其他模型数据一样。

基本上我想做的是,每当在基本模型上调用操作时,行为就使用工作流模型。例如,如果对帖子执行 edit() 操作,则帖子(附加行为)将使用其自己的模型名称、操作和 id 作为参数来触发工作流行为(例如 [Post, edit, 1]) 。然后,该行为将调用工作流模型的功能,该模型记录了在帖子上运行编辑时要执行的操作(例如,向订阅该帖子的用户发送电子邮件)并将执行该操作。

我的问题是,从行为中调用模型/控制器方法的正确方法是什么?从行为中使用的模型将始终是工作流,但该行为基本上应该可以在任何模型中使用(除了工作流本身)。我知道我可以直接从行为中运行 SQL 查询,但这当然不是 Cake 方式:-)

或者,我是否以错误的方式处理这个问题?我想在数据库中存储一定量的逻辑,以便不同用户可以轻松配置它,并且模型/控制器逻辑本身不需要无休止的配置检查,以便将来可以轻松添加/更改/删除工作流步骤。

I'm trying to write a behavior that will give my models access to a simple workflow engine I've devised. The workflow engine itself works as a CakePHP model, with workflow data stored in the database just as any other model data is stored.

Basically what I want to do is have the behavior use the workflow model whenever an action is called on the base model. For example, if the edit() action is executed for Posts, then the Post (with the behavior attached) will trigger the workflow behavior with its own model name, action, and id as arguments (e.g. [Post, edit, 1]). Then the behavior will invoke the functionality of the Workflow model, which has a record for what to do when edit is run on Posts (e.g. send e-mail to users who are subscribed to that post) and will carry that out.

My question is, what is the proper way to invoke model/controller methods from within the behavior? The model to be used from within the behavior will always be Workflow, but the behavior should be usable from basically any model (aside from Workflow itself). I know I could run SQL queries directly from the behavior, but of course this is not the Cake way :-)

Or, am I going about this in the wrong way? I want to store a certain amount of logic in the database so that it is easily configurable by different users, and not have endless configuration checks within the model/controller logic itself so that workflow steps can be easily added/changed/removed in the future.

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

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

发布评论

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

评论(1

冷月断魂刀 2024-09-10 08:33:14
App::import('Model', 'YourModel');
$obj = new YourModel();

http://book.cakephp.org/view/938/Loading-Models

App::import('Model', 'YourModel');
$obj = new YourModel();

http://book.cakephp.org/view/938/Loading-Models

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