蛋糕 PHP 1.3?我可以在任何控制器和视图中使用任何模型吗?
我的问题是举个例子。我必须在页面中显示所有类别的书籍和复选框,我希望将其发布到下一个视图而不与数据库交互,并且我想将它们存储在逗号分隔字符串的隐藏字段中。
实际上我有一个用于查找书籍的选项卡。用户单击它,它会列出我从类别模型和使用的类别控制器索引功能中获得的书籍类别列表。 我想将用户选择的类别发布到下一个视图,例如我将在其中获取有关来自另一个模型和控制器的书籍的用户过滤的更多信息。
实际上我想知道我必须做什么,我想开发这个找书功能。我是否应该有一个模型和一个控制器,他们使用一些表来找书,或者我可以在任何控制器中使用任何模型吗?功能。
My question is that with example. I have to show all categories of books and with checkboxes in a page which I want to be posted to next view without interacting db and there I want to store them in a hidden field in a comma separated string.
Actually I have a tab for find a book. user clicks on it and it has a list of categories of books listed which I get from categories model and used categories controller index function.
I want to post user selected categories to next view like where I will take more info regarding user filtration for books it is coming from another model and controller.
actually I want to know what I have to do I want to develop this find a book function.Should I have a model for this and a controller which they use some table for find a book or can I use any model in any controller for this function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Matiasf 的建议确实有效,但这不是推荐的做法。
如果将模型添加到 $uses 属性,您将为控制器中的每个操作加载这些模型,这需要更多的处理能力,并且可能会对站点的性能产生不利影响。
您可以在 CakePHP 文档中详细了解为什么这不是一个好主意。
更好的选择是使用 loadModel() 控制器方法。
例如:
您可以在文档中找到有关 loadModel 方法的更多信息:book.cakephp.org/view/977/Controller-Methods#loadModel-992
Matiasf's suggestion does work, but it is not the recommended way of doing things.
If you add models to the $uses property you'll be loading those models for every action in your controller, which requires more processing power and could have a detrimental effect on your site's performance.
You can read more about why this isn't a good idea in the CakePHP documentation.
The better option is to use the loadModel() controller method.
For example:
You can find more info about the loadModel method in the documentation: book.cakephp.org/view/977/Controller-Methods#loadModel-992
除了加载模型本身之外,您还可以在控制器中使用模型,只要两个模型之间存在关系即可。例如,考虑以下关系:
在这种情况下,您可以使用
针对您的案例,如果您发布数据库架构,将会有所帮助。尽管如此,要查找一本书,我假设您正在使用某些参数,例如类别、流派、出版商等。假设每个参数都有自己的数据库表和模型,您的搜索跃点如下:
第 1 页:带有检查的类别列表选择框
第 2 页:带有用于选择的复选框的流派列表
第 3 页:带有用于选择的复选框的出版商列表
第 4 页:根据给定参数过滤的书籍列表
我试图尽可能提供指导,如果您仍然不清楚,请发表评论如何去寻找书籍。如果您需要帮助将数据从一个控制器传递到另一个控制器,请阅读 CakePHP 手册 (http://book.cakephp.org/view/57/Controller-Methods) 或 Google 使用“cakephp 将数据从一个控制器传递到另一个控制器”作为关键词。
Apart from loading the model itself, You can use a model in a controller other than its own as long as there is a relation between the two models. For example, consider the following relation:
In this scenario, you can use
Coming to your case, it would help if you post your Database Schema. Nevertheless, to find a book, I assume you are using certain parameters like Categories, Genres, Publishers etc. Assuming each parameter will have it's own database table and model, your search hops would be as follows:
Page 1: List of Categories with Check boxes for Selection
Page 2: List of Genres with Check boxes for selection
Page 3: List of Publishers with Check boxes for selection
Page 4: List of Books filtered according to the given parameters
I tried to be as guiding as possible, leave comment if you are still not clear how to go about searching for books. If you need help with passing data from one controller to another, read the CakePHP Manual (http://book.cakephp.org/view/57/Controller-Methods) or Google using "cakephp passing data from one controller to another" as keywords.
关于标题中的问题:
是的,您可以在任何控制器中使用任何模型。设置
模型对象应该是可访问的
Regarding the question in the title:
Yes, you can use any model in any controller. Set the
And the model object should be accessible