蛋糕 PHP 1.3?我可以在任何控制器和视图中使用任何模型吗?

发布于 2024-10-01 02:55:37 字数 292 浏览 4 评论 0原文

我的问题是举个例子。我必须在页面中显示所有类别的书籍和复选框,我希望将其发布到下一个视图而不与数据库交互,并且我想将它们存储在逗号分隔字符串的隐藏字段中。

实际上我有一个用于查找书籍的选项卡。用户单击它,它会列出我从类别模型和使用的类别控制器索引功能中获得的书籍类别列表。 我想将用户选择的类别发布到下一个视图,例如我将在其中获取有关来自另一个模型和控制器的书籍的用户过滤的更多信息。

实际上我想知道我必须做什么,我想开发这个找书功能。我是否应该有一个模型和一个控制器,他们使用一些表来找书,或者我可以在任何控制器中使用任何模型吗?功能。

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 技术交流群。

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

发布评论

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

评论(3

忘东忘西忘不掉你 2024-10-08 02:55:38

Matiasf 的建议确实有效,但这不是推荐的做法。

如果将模型添加到 $uses 属性,您将为控制器中的每个操作加载这些模型,这需要更多的处理能力,并且可能会对站点的性能产生不利影响。

您可以在 CakePHP 文档中详细了解为什么这不是一个好主意

更好的选择是使用 loadModel() 控制器方法。

例如:

BooksController extends AppController {
    function index() {
        $this->loadModel('Category');
        $this->set('categories', $this->Category->find('all'));
    }
}

您可以在文档中找到有关 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:

BooksController extends AppController {
    function index() {
        $this->loadModel('Category');
        $this->set('categories', $this->Category->find('all'));
    }
}

You can find more info about the loadModel method in the documentation: book.cakephp.org/view/977/Controller-Methods#loadModel-992

夏九 2024-10-08 02:55:38

除了加载模型本身之外,您还可以在控制器中使用模型,只要两个模型之间存在关系即可。例如,考虑以下关系:

"Books" Model BelongsTo "Category" Model

在这种情况下,您可以使用

$this->Book->Category->find() inside BooksController 
as well as 
$this->Category->Book->find() inside CategoriesController

针对您的案例,如果您发布数据库架构,将会有所帮助。尽管如此,要查找一本书,我假设您正在使用某些参数,例如类别、流派、出版商等。假设每个参数都有自己的数据库表和模型,您的搜索跃点如下:

第 1 页:带有检查的类别列表选择框

  • 功能 - 类别控制器的 index()
  • 视图 - /app/views/categories/index
  • 传递到 - 流派控制器的 search()

第 2 页:带有用于选择的复选框的流派列表

  • 功能 - 流派控制器的 search()
  • 视图 - /app /views/genres/search
  • 传递至 - Publishers Controller 的 search()

第 3 页:带有用于选择的复选框的出版商列表

  • 功能 - Publisher Controller 的 search()
  • 查看 - /app/views/publishers/search
  • 传递至 - Books Controller 的 results()

第 4 页:根据给定参数过滤的书籍列表

  • 功能 - 书籍控制器的 results()
  • 视图 - /app/views/books/results
  • 传递到 - 无

我试图尽可能提供指导,如果您仍然不清楚,请发表评论如何去寻找书籍。如果您需要帮助将数据从一个控制器传递到另一个控制器,请阅读 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:

"Books" Model BelongsTo "Category" Model

In this scenario, you can use

$this->Book->Category->find() inside BooksController 
as well as 
$this->Category->Book->find() inside CategoriesController

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

  • Function - Category Controller's index()
  • View - /app/views/categories/index
  • Passes to - Genres Controller's search()

Page 2: List of Genres with Check boxes for selection

  • Function - Genre Controller's search()
  • View - /app/views/genres/search
  • Passes to - Publishers Controller's search()

Page 3: List of Publishers with Check boxes for selection

  • Function - Publisher Controller's search()
  • View - /app/views/publishers/search
  • Passes to - Books Controller's results()

Page 4: List of Books filtered according to the given parameters

  • Function - Book Controller's results()
  • View - /app/views/books/results
  • Passes to - None

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.

音盲 2024-10-08 02:55:38

关于标题中的问题:

是的,您可以在任何控制器中使用任何模型。设置

var $uses = array("Model_1", "Model_2", ...);

模型对象应该是可访问的

$this->Model_1->find(...);

Regarding the question in the title:

Yes, you can use any model in any controller. Set the

var $uses = array("Model_1", "Model_2", ...);

And the model object should be accessible

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