Is multi lavel join possible in laravel Eloquent? - php

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.

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

Best practice in CodeIgniter, when selecting data from multiple related models

I'm trying to figure out what is the best practice with selecting data from two related model.
I have a model (and a db table) "person" and then i have a model (and a db table) comment. There may be multiple comments for one person and comment has "person_id" column.
I have two cases in particular.
First i need to show persons profile with all his comments.
In my controller do I select the needed person through my person model and then select all the comments through my comments model? Or is it more correct for person model already return person with all comments?
I myself would guess that first option is ok.
Second case is where i need to show all latest comments with name of the person who made the comment.
So in my controller is it correct to select all latest comments from comments model and then select a person for each or them? Or is it more correct for the comments model to return comment with person name included?
In this case i would guess that second option is better. It seems really strange to first select comments and then iterate them and select a person for each of them.
So im kind of confused because case 1 and 2 seem similar but i would use different solutions for them. Which one is correct?

Query result as field in Doctrine entity

As part of a login system I have a User entity which I need to add 'Last Login' and 'Total Logins' information to.
I have a couple of tables to represent this, the user table and the user_log table. The user_log table captures other log actions, so I need to select only a specific type (LOGIN).
Basically I need to know how to express the following query in terms of doctrine entities:
SELECT username,
MAX(user_log.log_time) AS last_login,
(SELECT COUNT(id) FROM user_log
WHERE user_log.user_id = user.id
AND user_log.log_action = "LOGIN") AS login_count
FROM user
LEFT JOIN user_log ON user_log.log_action = "LOGIN"
AND user_log.user_id = user.id
I've tried using DQL, the problem is the DQL ran the correct query but it didn't work because I don't know how to create this kind of relationship in doctrine entities.
The only solution I can think of at the moment is to use the query above in a custom repository method and manually create / return the entities. If possible though I'd like to set up the relationships using annotations so that I can fetch this user information using the built in repository methods.
Any help would be greatly appreciated, thanks!

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.

Symfony - Using Outer Joins with Doctrine ORM

I have three entities: User, Answer, and Question.
There is a OneToMany relationship between User and Answer and a ManyToOne relationship between Answer and Question. Basically, a particular user optionally gives answers to a bunch of questions.
What I'm trying to accomplish in the world of ORM is retrieving ALL questions and their associated answers for a particular user. The key part is that a user may not have answered a particular question but I still want to get that question (with a null answer).
My Answer entity has a 'user' field which maps to the User entity which is inverted by an 'answers' field within the User entity. If I use this 'answers' field within the User entity, I only get the question/answer pairs for which the user has actually answered. I do not get questions for which the user has not answered.
Typically, using raw SQL, this would involve a simple "left outer join" between my question and answer table, but I want this to be done using Doctrine's ORM. Any suggestions? I'm pretty new to the world of ORM.
I did it! Here's how:
I created a field in my Question entity that contains all answers, from all users, for that particular question; its mapped with a OneToMany relationship to the Answer entity. And then to make sure we restrict that list to the answers for a particular user I created a custom repository for my Question entity and created the following function:
public function findAllJoinedToAnswer($user)
{
$query = $this->getEntityManager()
->createQuery('
SELECT q, a
FROM Bundle:Question q
LEFT JOIN q.answers a
WITH a.user = :user'
)->setParameter('user', $user);
try{
return $query->getResult();
}catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
Just pass in an instance of the User entity, and voila!

Categories