Reputation: 6355
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).
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?
Upvotes: 0
Views: 1371
Reputation: 1194
I saw your other post CakePHP Elements not updating table, so I sort of have an idea of your situation. It seems you are representing comments with your Post model.
You want to query data from your Post model, in your UploadsController, correct?
If your comments table is named comments
, you need to ensure it is associated to your Post model. Cake automatically associates Models and database tables if they follow Cake's naming conventions. But if they are in fact different, you can specify a custom database table for your Post model:
<?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 implies that the same Post can belong to many different Uploads. hasMany implies that a Post can only belong to a single Upload.
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);
}
...
}
?>
Upvotes: 1