P
P
ProFM2018-06-21 11:10:31
Yii
ProFM, 2018-06-21 11:10:31

How to link and access related tables in Yii2?

Good morning. Something I can’t understand in any way how to work with related tables, there are 3 tables:

social
5b2b5a5e8863b413244862.png
user
5b2b5a992e3e9149403343.png
social_users
5b2b5a8658dc9905999697.png

As you understand, I combined social and user into one table social_users so that later it would be more convenient to dynamically update the data. But here’s the problem, I just can’t figure out how to display data in a view from this table, so that all the data is automatically taken from related tables by their id ..... I’m sure that the answer is on the surface as usual, but I can’t find it, help please.
And here are the relationships in the models:
Social.php
public function getSocialUsers()
    {
        return $this->hasMany(SocialUsers::className(), ['social_id' => 'id_social']);
    }

user.php
public function getSocialUsers()
    {
        return $this->hasMany(SocialUsers::className(), ['user_id' => 'id_user']);
    }

SocialUsers.php
public function getUser()
    {
        return $this->hasMany(User::className(), ['id_user' => 'user_id']);
    }

    public function getSocial()
    {
        return $this->hasMany(Social::className(), ['id_social' => 'social_id']);

    }


In views:
ProfileController.php
public function actionIndex()
    {
        $id = Yii::$app->user->identity['id_user'];
        $profile = Profile::find()->where(['user_id' => $id])->asArray()->one();
        $socials = SocialUsers::find()->asArray()->all();

        return $this->render('index', [
            'profile' => $profile,
            'socials' => $socials,
        ]);
    }
index.php
<?php if(isset($socials)):?>
            <div class="list-group">
        <?php foreach ($socials as $social):?>
                <a href="#" class="list-group-item"><i class="fa fa-facebook"></i> </a>
        <?php endforeach;?>
            </div>
    <?php endif;?>

I get:
site
5b2b5d147095b239134046.png

I understand that I generally get all the data from the table, and they are in no way tied to either the user or the table with social networks. How do these connections work in general, and why are they?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2018-06-21
@kimono

class Social extends ActiveRecord {
  public function getPivotSocialUser(){
    return $this->hasMany(SocialUser::class, ['social_id' => 'id');
  }
  public function getUsers(){
    return $this->hasMany(User::class, ['id' => 'user_id')->via('pivotSocialUser');
  }
}

class User extends ActiveRecord {
  public function getPivotSocialUser(){
    return $this->hasMany(SocialUser::class, ['user_id' => 'id');
  }
  public function getSocials(){
    return $this->hasMany(Social::class, ['id' => 'social_id')->via('pivotSocialUser');
  }
}

view.php
$social = Social::findOne(['name' => 'facebook']);
var_dump($social->users); // все пользователи фейсбука
$user = User::findOne(['name' => 'admin']);
var_dump($user->socials); // все социальные сети админа

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question