D
D
dake12312015-10-02 12:26:40
Yii
dake1231, 2015-10-02 12:26:40

Related tables in Yii2, what's wrong?

Hello! Application on Yii2, Oracle database.
There are tables:

  1. projects
  2. user (Users)
  3. user_projects (Users in the project) many to many fields - project_id, user_id

I link them in the Projects model like this
public function getUserProjects()
    {
        return $this->hasMany(UserProjects::className(), ['project_id', 'id']);
    }

public function getUsers()
    {
        return $this->hasMany(User::className(), ['id', 'user_id'])->via('userProjects');
    }

as a result, if you do $model->users, it gives an error Getting unknown property: common\models\Projects::project_id.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LAV45, 2015-10-07
@LAV45

public function getUserProjects()
{
    return $this->hasMany(UserProjects::className(), ['project_id' => 'id']);
}
public function getUsers()
{
    return $this->hasMany(User::className(), ['id' => 'user_id'])
        ->via('userProjects');
}

I recommend using Gii and then there will be no such problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question