Welcome Guest, Not a member yet? Register   Sign In
AR get own posts and friends posts
#1

[eluser]tommyadey[/eluser]
Hey guys, I have these tables;
Code:
user - contains user_id | username | fullname | email etcc
user_followers - contains follow_id| user_id | follower_id | date etcc
posts - contains post_id | user_id | post | post_date
I'm trying to grab all the posts by the users the user is following and the user's own posts.

I've tried
Code:
$this->db->select('posts.*')->from('posts')
            ->join('user_followers', 'user_followers.user_id = posts.user_id', 'INNER')
            ->where('user_followers.follower_id', $user_id)->order_by('posts.post_date','desc');
        $query = $this->db->get();
But the problem is, i'm not able to get the user's posts from this query. I've tried other methods with or_where etc and i was able to get the user's posts, only that the data was tripled Sad
Can someone please help me out here?
Many Thanks in advance.
#2

[eluser]adamck[/eluser]
can you provide a .SQL export of you tables, this way i can import them to Phpmyadmin and test some queries.
send it to my email, [email protected]
#3

[eluser]tommyadey[/eluser]
Anyone?
#4

[eluser]jprateragg[/eluser]
I'm not familiar enough with AR to put your query in that format, but you could just use a regular SQL query:

Code:
SELECT p.post_title, p.post_date, u.username
FROM posts p
LEFT JOIN users u ON u.id = p.user_id
WHERE p.user_id = 2 OR p.user_id IN (SELECT user_id FROM user_followers WHERE follower_id = 2)

This would probably do what you want it to do.
#5

[eluser]adamck[/eluser]
Just tested this

Code:
SELECT posts.*
FROM `posts`
WHERE posts.user_id = '33'
OR posts.user_id IN (SELECT follower_id FROM `user_followers` WHERE user_followers.user_id = '33')

This gets all the posts that user 33 have made and all the posts from the people he's following
#6

[eluser]tommyadey[/eluser]
Many thanks jprateragg and adamck, both worked perfectly, is there a way to use subquery/subselect in active record?




Theme © iAndrew 2016 - Forum software by © MyBB