如何在 CakePHP 中的模型函数内使用助手
现在,在你把我烧死在火刑柱上之前,请听我说完!
我希望产品描述字段的一些关键字链接到其他产品(有点像 mediawiki 链接),但是在某些时候我需要建立这些关联并将关键字链接起来,所以我需要对每个卷曲进行搜索 -我在描述中找到大括号词并生成描述的格式化版本,以减少每次显示描述时处理这些关键字链接的次数。
为了方便/一致性,我使用自定义帮助程序创建所有产品链接,我所需要做的就是传递产品行,并且帮助程序为我提供带有我指定的任何选项的链接。唯一的问题是,我现在需要在 beforeSave() 中执行此操作,以便我可以填充 description_formatted。
目前, beforeSave() 检查原始描述行,然后调用模型中与每个关键字匹配的私有方法,查询数据库中的匹配行......这就是我所得到的。
Now before you burn me at the stake hear me out!
I want some keywords of a product description field to link to other products (kinda like mediawiki links), however at some point I need to make these associations and link the keywords up, so I'll need to do a search on each curly-braced word I find in the description and produce a formatted version of the description to cut down on processing these keyword links every time the description is displayed.
For ease/consistency I am creating all product links with a custom helper, and all I need to do is pass the product row in and the helper products a link for me with any options I specify. The only this is, is that I need to now do this in beforeSave() so I can populate description_formatted.
At the minute, beforeSave() checks for the original description row, then calls a private method in the model which matches each keyword, queries the db for a matching row... that is as far as I've got.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像任何其他 MVC 一样,Cake 对如何耦合类做出了很大的限制。这是为了让脚本小子搬起石头砸自己的脚。然而,如果你真的需要相处,蛋糕有一个利基解决方法:http: //book.cakephp.org/view/933/The-App-Class
Just like any other MVC, Cake makes big restrictions how to couple your classes. This is needed to keep script kiddies to shoot themselves in the foot. However, there is a niche workaround for cake if you really need to get along: http://book.cakephp.org/view/933/The-App-Class
我该怎么做呢?使用助手,我会将所有花括号单词替换为链接,当用户将鼠标悬停在链接单词上时,我将调用 Ajax,它将获取单词描述或链接或您需要执行的任何操作。这样,您仅在需要时才请求描述。
如果您仍然坚持使用帮助程序 - 它只是 PHP 中的一个类,因此您可以将其包含在模型中,创建该类的对象并使用它的函数。
第三个选项是创建您自己的类并在模型和帮助程序中使用它。
How I would do this? With the helper I would replace all curly braces words into links and when the user hovers the linked word I would call an Ajax which will get the word description or the link or whatever you need to do. This way you request the description only when it's needed.
If you still insist to use the helper - it's just a class in PHP, so you can include it in your Model, create an object of the class and use it's functions.
The third option is to create your own class and use it both in Model and the Helper.