CakePHP:在单个页面中检索和组合多个模型数据

发布于 2024-12-21 07:53:15 字数 350 浏览 2 评论 0原文

这是一个非常普遍的 Cakephp 问题。我试图将来自许多不同模型的模型数据组合到给定页面中。

我正在为我老板的在线简历构建一个简单的内容管理器,以便他可以通过一个简单的 GUI 来管理它。因此,我拥有适用于简历所有不同部分的模型,但需要在单个页面上或通过单个控制器方法组合来自每个模型的数据。

我的直觉是,我要么必须为“cv”构建一个模型(尽管永远只有一个),并将适当的 hasMany/belongsTo 关联分别构建到“cv”和内容模型中,要么构建一个能够单独向模型控制器发出请求的静态页面。考虑到项目很小,前者似乎是不必要的工作密集型,但在后一种情况下,我不知道如何向控制器发出请求以向页面提供模型数据。我确信这非常简单,我只是找不到答案!谢谢!

This is a very general Cakephp question. I'm attempting to combine the model data from many different models into a given page.

I'm building a simple content-manager for my boss's online-CV so he can manage it from a simple GUI. So I have models for all the different sections of the CV, but need to combine the data from each model on a single page or via a single controller method.

My intuition is that I've either got to build a model for 'cv' (though there will only ever be one) and build the appropriate hasMany/belongsTo associations into a 'cv' and the content models respectively, or else to build a static page that is capable of making requests of model controllers individually. The former seems unnecessarily work-intensive given how small the project is, but in the latter case I don't know how to make requests of controllers to provide model data to a page. I'm sure this is dead simple, I just can't find the answer! Thanks!

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

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

发布评论

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

评论(1

绮筵 2024-12-28 07:53:15

您可以创建一个 DashboardsController(或任何您想调用的名称),然后在 Dashboard 模型中指定不需要数据库表: var $useTable = false;

在 Config/在routes.php文件中,添加: Router::connect('/', array('controller' => 'dashboards', 'action' => 'index'));您的主页(如果您愿意的话)。

然后,在仪表板控制器的索引操作中,您可以使用 $this->loadModel('Whatever');,然后就可以从该模型获取数据了: $ myData = $this->Whatever->find('all');.您可以根据需要加载尽可能多的模型。

TLDR/简化版:

1) Make Dashboard controller with 'index' action
2) Make Dashboard model and specify: var $useTable = false;
3) Set Route to use your Dashboard controller for homepage (or any other page: 
  `Router::connect('/', array('controller' => 'dashboards', 'action' => 'index'));`
4) Use $this->loadModel('Whatever'); to gain access to that model's methods

You can make a DashboardsController (or whatever you want to call it), then in the Dashboard model, you specify that you don't need a database table: var $useTable = false;

In the Config/routes.php file, add: Router::connect('/', array('controller' => 'dashboards', 'action' => 'index')); to make that your homepage (if you want to).

Then, in the Dashboard controller's index action, you can use $this->loadModel('Whatever');, and you're good to go to get data from that model: $myData = $this->Whatever->find('all');. You can load as many models as you'll need the data for.

TLDR / simplified:

1) Make Dashboard controller with 'index' action
2) Make Dashboard model and specify: var $useTable = false;
3) Set Route to use your Dashboard controller for homepage (or any other page: 
  `Router::connect('/', array('controller' => 'dashboards', 'action' => 'index'));`
4) Use $this->loadModel('Whatever'); to gain access to that model's methods
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文