M
M
Maxim Grechushnikov2015-06-03 17:10:29
Yii
Maxim Grechushnikov, 2015-06-03 17:10:29

Yii2 Relations: Confused. A simple relay on itself. Can you help?

There is a system user model. Each user with the corresponding status can assemble a team from the same users. The task seems to be simple. And here's something I got confused in the relays.
At the moment, there are 2 implementations that are not working:
1. User model and users_teams crosstab (team_id, user_id) team_id is the owner's user id and user_id is the user's id, incl. and team owner.
This option seems to have started with the owner, but after selecting a participant, he no longer has a team.
2. The User model and the field in the team_id model
also do not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Belikov, 2015-06-03
@maxyc_webber

If I understand correctly, it should look something like this.
Users

// Таблица
// user
// id | name

// Модель
class User {
  public function getTeam() {
      return $this->hasOne(UserTeam::className(), ['id' => 'user_id']);
  }
}

Teams
// Таблица
// team
// id | name

// Модель
class Team
{
  public function getUsers() {
      return $this->hasMany(UserTeam::className(), ['id' => 'user_id']);
  }
}

Crosstab
// Таблица
// user_team
// id | team_id | user_id

// Модель
class UserTeam {
  public function getUser() {
      return $this->hasOne(User::className(), ['id' => 'user_id']);
  }
  public function getTeam() {
      return $this->hasOne(Team::className(), ['id' => 'team_id']);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question