2

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()
codex
  • 432
  • 2
  • 7
  • 18

1 Answers1

2

If you have a relation name referrals (getReferrals() ) you should use referrals in your joinWith

$query = User::find()->joinWith('referrals');
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • I do have a relation called referrals in the database side. I thought the raw tables from the database is already discarded and that you should use the models instead. It's working now thank you! – codex Mar 26 '18 at 16:55
  • You mean a view in database ?? .. or a foreignKey ? .. anyway Yii2 use the function provided as getter in model with hasMany( .) or hasOne() as relation .. .. – ScaisEdge Mar 26 '18 at 16:57
  • I just realized that it gets the returned value of the function tablename() for the `joinWith` statement that is why it has to be `referrals` since it's the returned value of the tablename() function... *facepalm – codex Mar 26 '18 at 17:01