CakePHP Model association for a social networking website - php

I'm sorry. I thought about it but I can't come to a better title since I don't know how to describe this problem in one sentence.
Anyway, I am building a news feed just as Facebook where users can post a wall message and other users can comment on it.
I have 3 Tables in the database
profiles Table which contains the file name for their profile picture. posts table which contains details about posts and comments table for comments.
I have the below code in my Post model
public $belongsTo = 'Profile';
public $hasMany = 'Comment';
by doing that I can get the comments associated with each posts. But I am not able to get the profile picture from profiles table for "commenter". I tried
public $belongsTo = 'Profile;
in the Comment model too but it's not working.

Related

Eloquent Relationships: user hasmany posts has many comments

I'm using laravel and i am trying to create my database with Users, this user will have many comments and many posts, the catch is that the post also need to have many comments that all will belong to a post. And that the comments from the user will belong to a post.
Maybe im thinking about it in the wrong way, but cant figure it out.
Help is apriciated
A comment can belongTo both a Post and a Comment. So, a solution could look something like this:
Relationships
User
hasMany Post
hasMany Comment
Post
belongsTo User
hasMany Comment
Comment
belongTo User
belongsTo Post
Models
User
id
Post
id
user_id
Comment
id
user_id
post_id

Is multi lavel join possible in laravel Eloquent?

I have Article model join with User model and Comment model, Comment model join with user when I retrieve article with user and comments, it works but it wont retrieve user data who commented.
This is my code to retrieve article data
$articleData = $this->article->with('user','comments')->find($id);
I want to retrieve article with user and comments - (with user).
I just found the answer for my question
$articleData = $this->article->with('user','comments.users')->find($id);
This will retrieve user data who commented on article.

Update parent model from child (belongsto) model

I have Posts and Comments and users can "Like" either. I'm using cakePHP.
The Posts and Comments tables each have a 'likes' row on them because I don't want to re-count the likes each time the post / comments are loaded. I have a Likes table too, that contains the IDs (post id, user id) so that I know what users have already 'liked' something.
I was wondering how I would set up this relationship within the models in cakePHP and also how I would update Posts.likes field when at the same time adding a new like into the Likes table.
I've set up Likes to "belongTo" Posts and Comments in the Like Model and at the moment, my LikesController.php looks like this:
public function add(){
...
if ($this->Like->save($this->request->data)) {
//like is added to Likes table, now how to add to the "parent" Post or Comment??
}
...
}
Keep your tables as they are, but add a like_count field to your posts and comments tables.
Also add a comment_count to the posts table.
Then just use CakePHP's counterCache, and it will keep track of the # of likes and comments per post automatically.

How can this association be set up?

I'm using the cakephp framework to develop an application and I'm running into some trouble understanding the associations between these models fully. Below you can see the four models along with their relative database fields.
User
id
Profile
id
user_id
Post (A blog post on the users profile)
id
profile_id
topic_id
Topic (A topic for a blog post)
id
name
Here are the associations as they currently stand:
User
hasOne: Profile
Profile
hasMany: Posts
Post
belongsTo: Topic, Profile
Now my problem. I am unsure if you have to define associations like User hasMany Posts or if it's already assumed because User hasOne Profile and Profile hasMany Posts. My other problem is defining the relationship between a post and its topic.
A profile can have unlimited posts
A post must be associated with a profile
A post can only have one topic
The topic table contains a list of all topics
A post does not NEED a topic
Given these criteria how should my associations look? All the research I've done on associations only shows simple examples.
I'm using CakePHP version 2.1.3
Thanks for any and all help and/or advice in advance
You can recursively find associations of associations, or even better, use Containable.
In the model (I recommend putting it in AppModel, since I find myself using Containable for everything):
class AppModel extends Model {
public $actsAs = array('Containable');
...
}
Then when you call read (or find, or paginate) for User, most likely in your controller, do this:
$this->User->contain(array(
'Profile' => array(
'Post'
)
));
$data = $this->User->read();
$set('user',$data);
If you set that data to your view, you can then access the id of one of the posts from $user['Profile']['Post'][0]['id'].
Now for your next question, you can have conditional associations.
public $hasMany = array(
'Topic' => array(
'className' => 'Topic',
'conditions' => 'Post.topic_id IS NOT NULL'
)
)
I think everything looks fine
Your assumption is correct you dont have to define the User / Post relationship. Users dont have many Posts, Profiles do. You could store the user_id on the Post rather than profile_id to make thing a bit more intuitive but thats up to you.
Topic hasMany Post and you are done. The topic/post conditions you describe can be controlled via the forms and before saves on the model. For example 'A post must be associated with a profile', well at the point you save the post you add in the profile_id based on session info of the logged in user.

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).
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. 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);
}
...
}
?>

Categories