Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

LEFT JOIN not returning anything

I tested this query in SQL and it worked perfectly for returning all of the posts and if they are liked by member:

SELECT blogposts.*, bloglikes.* FROM blogposts LEFT JOIN bloglikes ON blogposts.postId = bloglikes.postId AND blog_likes.memberId = 5

I tried refracting it for use with PHQL:

SELECT Posts.* , Likes.* FROM Posts LEFT JOIN Likes ON Posts.postId = Likes.postId AND Likes.memberId = 5

I have created the relationships already inside the models, but for some reason it won't return anything. Using the query below will actually work by returning if it is liked or not but it won't return the post.

SELECT Posts.*, Likes.likeId, Likes.memberId FROM Posts LEFT JOIN Likes ON Posts.postId = Likes.postId AND Likes.memberId = 5

The above statement returns likeId and memberId but no post information like title, body, etc. Have any assumptions to why this isn't working?

cant really say if anything is wrong there but i suggest you log your query and see what SQL query does it generate: http://docs.phalconphp.com/en/latest/reference/models.html#logging-low-level-sql-statements

I tried the query that it logged and it worked in Terminal. Still have no idea what the problem could be.



5.7k
Accepted
answer

I figured out the issue. When you use select all from Posts, you have to explicitly state the column names you want to select. You can't use Posts.* but Posts.title, Posts.body, etc.