I have 2 models created called User and ReferralsForm where Users is the parent and ReferralsForm is the child. There are two foreign keys I am assigning to ReferralsForm from Users but with different names. I am using Gii to generate my models along with the relations. Here is the structure:
User
------------
users_id(PK),
display_name,
username,
email,
password,
member_since,
referral_hash
ReferralsForm
-------------
referral_id(PK),
userid(FK),
referred_user_id(FK),
status
User Model Relation code and attributelables
public function getReferrals()
{
return $this->hasMany(ReferralsForm::className(), ['userid' => 'user_id']);
}
public function getReferrals0()
{
return $this->hasMany(ReferralsForm::className(), ['referred_user_id' => 'user_id']);
}
public function attributeLabels()
{
return [
'user_id' => 'Userid',
'username' => 'Username',
'email' => 'Email',
'display_name' => 'Display Name',
'password' => 'Password',
'member_since' => 'Member Since',
'auth_key' => 'Auth Key',
'referral_hash' => 'Referral Hash',
];
}
ReferralsForm Model Relation code and attributelabels
public function getUser()
{
return $this->hasOne(User::className(), ['user_id' => 'userid']);
}
public function getReferredUser()
{
return $this->hasOne(User::className(), ['user_id' => 'referred_user_id']);
}
public function attributeLabels()
{
return [
'id' => 'ID',
'userid' => 'Userid',
'referred_user_id' => 'Referred User ID',
'subscription_id' => 'Subscription ID',
'status' => 'Status',
];
}
Here is my controller:
public function actionReferrals()
{
$query = User::find()->joinWith('ReferralsForm');
$model = new User();
$ref_hash = $model->getHash();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $this->render('referrals' ,['dataProvider' => $dataProvider, 'ref_hash' => $ref_hash]);
}
Still it gives me an error
Invalid Argument – yii\base\InvalidArgumentException
app\models\User has no relation named "ReferralsForm".
Caused by: Unknown Method – yii\base\UnknownMethodException
Calling unknown method: app\models\User::getReferralsForm()