使用一种模型读取数据,使用另一种模型保存数据

发布于 2024-10-06 09:30:35 字数 692 浏览 0 评论 0原文

我有一个名为 google_news.php 的模型,它使用外部数据,另一个模型 saved_news.php 使用数据库中的 saving_news 表,

在我的控制器中,我声明我使用这两个模型:

var $uses = array('GoogleNews', 'SavedNews');

我的索引函数读取数据:

$this->set('news',$this->GoogleNews->find('all'));

我的视图如下所示:

<?php foreach( $news as $newsItem ) : ?>

<?php echo $html->link($newsItem['GoogleNews']['title'], array('action'=>'add',    $newsItem['GoogleNews']['title'])); ?>

<?php echo $newsItem['GoogleNews']['encoded']; ?>

<em>
<hr>

<?php endforeach; ?>

如何在我的控制器中编写添加函数以将每个数据保存到我的数据库中?

I have a model named google_news.php which uses the external data, and another model saved_news.php which uses my saved_news table in database,

In my controller I declared that Im using this two models:

var $uses = array('GoogleNews', 'SavedNews');

and my index function reads data:

$this->set('news',$this->GoogleNews->find('all'));

and my view looks like this:

<?php foreach( $news as $newsItem ) : ?>

<?php echo $html->link($newsItem['GoogleNews']['title'], array('action'=>'add',    $newsItem['GoogleNews']['title'])); ?>

<?php echo $newsItem['GoogleNews']['encoded']; ?>

<em>
<hr>

<?php endforeach; ?>

How to write the add function in my controller to save each data to my database?

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

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

发布评论

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

评论(1

残疾 2024-10-13 09:30:35

您应该将需要保存到 $this->data['ModelName'] 中的内容指定为字段数组。看看书中的保存数据。这将详细解释需要遵循的格式。

You should assign what you need to be saved into $this->data['ModelName'] as array of fields. Take a look at saving data in the book. That will explain more about the formatting that needs to be followed.

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