CakePHP - 访问多个表的页面类别
我正在制作一个具有许多不同类别的网站,每个类别都有自己的控制器/模型/视图。
示例网站类别:餐饮、电影、夜生活、体育...等
我的控制器:餐馆、剧院、活动、文章、城市、用户...等等
我还有一个网站主页,可以从所有这些类别中提取数据来填充少量内容页面上的框。而且,每个类别都有自己的主页,也可以从不同类别中提取数据 - 例如,在“电影”主页上,它还可能提取剧院附近的餐馆和体育赛事,也许还有文章中的一些最近的文章模型...等等-有很多类别重叠。
我该如何设置?体育赛事根本与电影无关,因此我无法想象任何 hasMany/belongsTo 类型的关系 - 但我仍然需要访问数据。尤其是在主页上,实际上根本没有表格,但需要从以上所有数据中提取数据。
I'm making a site that has a lot of different categories, each with their own controller/model/views.
Example site categories: Dining, Movies, Nightlife, Sports...etc
My Controllers: restaurants, theaters, events, articles, cities, users... and more
I also have a site homepage that pulls data from all of them to populate little boxes on the page. And, each category has it's own homepage that also could pull data from different categories - for instance, on the "Movies" homepage, it might also pull in restaurants and sporting events that are nearby the theaters and maybe a few recent articles from the articles model... etc etc - there is a lot of category-overlap.
How do I set this up? The sporting event isn't tied to the movie at all, so I can't imagine any hasMany/belongsTo type relationships - but I still need to access the data. And especially on the homepage, which really has no table at all, but needs to pull data from all of the above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的问题太大了,无法全部涵盖。我将解决跨不同控制器访问多个模型的问题。这可以通过多种方式完成。
通过关系:
通过将其添加到控制器中的
$uses
属性:通过根据需要加载到方法中:
每个都有其自己的优点。您应该阅读有关CakePHP 书中的模型的更多信息。如果您有大量关系,您应该查看 LazyModel 用于延迟加载模型。
Your problem is too big to cover in its entirety. I'll address accessing multiple models across different controllers. This can be done in several ways.
by relationship:
by adding it to the
$uses
property in your controller:by loading into the method as needed:
Each has its own benefits. You should read more about Models in the CakePHP Book. If you have a large amount of relationhships, you should check out LazyModel for lazy loading your models.
听起来您需要实现
组件
来处理各种类别。该组件将提供对任何给定页面(与类别主页不同)的类别框的访问。然后我将实现一个名为“类别”的元素,您可以将信息从组件传递到该元素。我还会使所有组件导出类似的数据。这将使其变得简单,但仍然提供您所寻求的灵活性。
总之,为每个类别创建组件:
将输出传递到要在页面上显示的类别元素。
Sounds like you need to implement
components
to handle the various categories. The component will provide the access for the category box for any given page (which will be different from the category home page).Then I would implement an element called 'category' that you can pass the information to from the component. I would also make all components export similar data. This will keep it simplified but still provide the flexibility you are looking for.
In summary, create components for each category:
Pass the output to the category element to be displayed on the page.
乍一看,您可能没有看到另一个总体模型,但是您将它们称为“类别”的想法意味着它们必须是某种事物(可能是活动)的类别。新模型可能非常薄,但我可能怀疑诸如外观安排之类的东西与活动模型有关,因此您可以为其指定开始和结束发布日期。如果您不想将其称为“活动”,也许像“contentItem”这样的通用内容会给您提供控制全局属性的地方。另一个练习是比较现有模型并查找重复的属性。这些重复的属性描述了什么?
At first glance maybe you don't see another model that is overarching, but the idea that you are calling them "categories" means they have to be a category of something, possibly activities. The new model may be pretty thin, but I would maybe suspect something like appearance scheduling to be involved with the activity model, so you can give it a beginning and ending publish date. If you don't want to call it an "activity", maybe something generic like "contentItem" would give you somewhere to control global attributes. Another exercise would be to compare your existing models and look for duplicate attributes. What are those duplicate attributes describing?
如果两个控制器之间没有关系,您可以使用 loadModel 函数
当然,当存在关系时,你可以这样做
You could use the loadModel function if there is no relation between two controllers
When there is a relation of course, you can do
这可能就是您想要的
如何为您的 cakephp 应用程序构建仪表板
This is probably what you want
How to build a dashboard for your cakephp application
要了解有关 CakePHP 中数据模型的更多信息,您可以使用 http://cakeApp.com 在线架构构建器。
To understand more about the data models in CakePHP you can use http://cakeApp.com a online schema builder.