I
I
Ivan Lykov2018-04-14 17:55:27
Yii
Ivan Lykov, 2018-04-14 17:55:27

How to display the number of comments on one post while still displaying who left?

Guys, good time.
I have a question. The task is:
There is a personal account, in the personal account (on the site) there are posts, you can leave comments on these posts.
I display posts and link them to the user's profile.
But I can't figure out how to properly link other users tobish their comments tobish to one post. And display under the post?
The table structure is
comments
5ad21374b7c29096013978.png
user_posts
5ad21393916af362021431.png
Then in the code I make relational links to all models
User_profile
5ad2181bcda71196891241.png
User_post
5ad21822bdf83223855351.png
Comments
5ad2182946678607392038.png
Here are the links themselves
, then I make a request, I hang up a getter which actually already has comments and a record in it. I pass it to the view. I pick up the user id to verify that this is his entry - a post.

$resultPostUser = UserProfile::find()
            ->asArray()
            ->with('posts')
            ->where(['user_id' => $my_id])   
            ->one();

After this code, I make a dump and get such a picture
5ad2152be597e793112681.png
. Green - this is data from the profile
. Red - the post that matches the profile of user
A. Orange is the comment that is in this post. But there is one thing, but, how much is MY comment and only mine displayed in the commentary, there should seem to be like other id_user.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Lykov, 2018-04-14
@Jhon_Light

$queryPostUser->from(UserPosts::tableName()." us_post")
            ->select('us_prof.user_id, us_prof.avatar, user.username, us_post.text, us_post.id, com_us.text as comment')
            ->leftJoin(UserProfile::tableName()." us_prof",'us_prof.user_id = us_post.id_user')
            ->leftJoin(User::tableName()." user",'user.id = us_post.id_user')
            ->leftJoin(Comments::tableName()." com_us",'com_us.id_post = us_post.id')
            ->where(['us_post.id_user' => Yii::$app->user->identity->getId()]);

Bottom line, in order to select all entries and comments to them, I did this.
Thanks everyone for the support and advice

D
Dmitry, 2018-04-14
@slo_nik

Good evening.
Links to comments should be hasMany(), you most likely have hasOne().
Complete your question with links, and we'll see.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question