gpt4 book ai didi

cakephp - 如何将多个模型 ID 传递到 CakePHP 中的注释添加操作?

转载 作者:行者123 更新时间:2023-12-02 11:00:25 24 4
gpt4 key购买 nike

我的问题涉及如何将一个评论 Controller 与多个“属于” Controller 一起使用。

我想向新闻、帖子和比赛添加评论(这是一个网球网站)。我已经设置了评论添加操作以使用“ajax”助手,并且还使用 counterCache 来跟踪每条新闻、帖子和比赛的评论数量。

我不知道的是如何将唯一 ID(新闻、帖子、匹配)传递到评论 Controller 添加操作中,以便表单保存评论和 news_id、post_id 或 match_id,以表单中传递的为准。

我的想法是,表单是否设置为收集唯一 ID,以便传递的内容特定于该 Controller (新闻、帖子、比赛)。结果是添加操作添加评论以及新闻/帖子/匹配 ID 值。

否则我必须设置一个 post_comments、news_comments 和 match_comments Controller 来处理每个 Controller ,而这不会是 DRY我所追求的方法。

我仍在积累对 CakePHP 的知识和理解。我怎样才能实现我设定的目标?

最佳答案

您的评论数据库表应如下所示:

id | belongs_to | foreign_id | ....
-------------------------------------
| | |

belongs_toenumvarchar

然后在你的模型中你可以像这样关联评论模型在评论模型中:

var $belongsTo = array(
'News' => array(
'className' => 'News',
'foreignKey' => 'foreign_id',
'conditions' => array('Comment.belongs_to'=>'News'),
'fields' => '',
'order' => ''
),
'Post' => array(
'className' => 'Post',
'foreignKey' => 'foreign_id',
'conditions' => array('Comment.belongs_to'=>'Post'),
'fields' => '',
'order' => ''
)
);

在后模型中:

var $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'foreign_id',
'dependent' => true,
'conditions' => array('Comment.belongs_to' => 'Post'),
'fields' => '',
'order' => 'Comment.created DESC',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);

关于cakephp - 如何将多个模型 ID 传递到 CakePHP 中的注释添加操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2275594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com