简体   繁体   English

CakePHP 2.0-2.1,从控制器查询不同的模型

[英]CakePHP 2.0 - 2.1, Query different model from controller

I've got an uploads controller which handles video file formats. 我有一个上载控制器,可以处理视频文件格式。 In the uploads controller you are able to browse all the videos uploaded and watch them. 在上传控制器中,您可以浏览所有上传的视频并观看。 In the function watch I have a comments element which updates the comments table with the userid (comment left by user that is currently logged in) uploadid (uploaded that is currently being watched) and the comment (comment of the user). 在功能表中,我有一个comment元素,它用userid(当前登录的用户留下的评论),uploadid(当前正在观看的已上传的评论)和comment(用户的评论)来更新评论表。

I've got the form working perfectly, however I'm not to sure how I would acquire the information from the comments table, such as the necessary query to be implemented? 我的表单运行正常,但是我不确定如何从注释表中获取信息,例如要实施的必要查询?

Any suggestions? 有什么建议么?

I saw your other post CakePHP Elements not updating table , so I sort of have an idea of your situation. 我看到您的其他文章CakePHP Elements未更新表 ,所以我有点了解您的情况。 It seems you are representing comments with your Post model. 看来您是用Post模型代表评论。

You want to query data from your Post model, in your UploadsController, correct? 您要在您的UploadsController中从Post模型查询数据,对吗?

If your comments table is named comments , you need to ensure it is associated to your Post model. 如果您的评论表名为comments ,则需要确保它与Post模型相关联。 Cake automatically associates Models and database tables if they follow Cake's naming conventions. 如果Cake和Model和数据库表遵循Cake的命名约定,它们会自动将它们关联起来。 But if they are in fact different, you can specify a custom database table for your Post model: 但是,如果它们实际上不同,则可以为Post模型指定一个自定义数据库表:

<?php
class Post extends AppModel {

    var $useTable = "comments" /*Or whatever you named your comments table*/

    ...
}
?>

You also have to ensure the model associations are set up between Post and Upload: 您还必须确保在发布和上传之间设置了模型关联:

Post belongsTo Upload
Upload hasMany Post

I noticed you have: 我注意到您有:

Post belongsTo Upload
Upload hasAndBelongsToMany Post

Is there a reason why it is HABTM? 它为何是HABTM? HABTM implies that the same Post can belong to many different Uploads. HABTM表示同一个帖子可以属于许多不同的上载。 hasMany implies that a Post can only belong to a single Upload. hasMany意味着一个帖子只能属于一个上载。

Finally, now that the model associations are set up, you can access related models in a controller: 最后,既然已经建立了模型关联,则可以在控制器中访问相关模型:

<?php
class UploadsController extends AppController {

    ...

    function watch ($id = null) {

        $this->Upload->id = $id;      
        $this->set('uploads', $this->Upload->read());

        /* To get related comments (Posts) */
        $related_comments = $this->Upload->Post->find('all', array(
            'conditions' => array(
                'Upload.id' => $id /* This condition makes it so only associated comments are found */
            )
        ));
        $this->set('comments', $related_comments);
    }

    ...

 }
 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM