user827391012 user827391012 - 1 month ago 16
PHP Question

joinWith multiple tables yii 2

Hi is possible to join multiple table based on a column in yii 2? Right now I am using the concept of

joinWith


Take a look at my code:

ActiveCurriculum.php
This is where I am joining the tables

public static function Addblock($group, $clientid){
$subjects = ActiveCurriculum::find()
->select(['scstock.*', 'schead.*', 'glhead.*', 'glfees.*'])
->joinWith('schead')
->joinWith('glhead')
->joinWith('glfees')
->where([
'scstock.sectiongroup' => $group
])
->asArray()
->all();
}


And these are the relations that I set:

Scstock

<?php
namespace app\models;
use Yii;

use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

class Scstock extends ActiveRecord{
public static function tableName()
{
return '{{%scstock}}';
}

public function getSchead(){
return $this->hasOne(Schead::className(), ['TrNo' => 'TrNo']);
}

public function getGlhead(){
return $this->hasOne(Glhead::className(), ['TrNo' => 'TrNo']);
}

public function getGlfees(){
return $this->hasOne(Glfees::className(), ['TrNo' => 'TrNo']);
}


}?>


Schead

<?php
namespace app\models;
use Yii;

use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

class Schead extends ActiveRecord{
public static function tableName()
{
return '{{%schead}}';
}

public function getScstock(){
return $this->hasOne(ActiveCurriculum::className(), ['TrNo' => 'TrNo']);
}

public function getGlhead(){
return $this->hasOne(Glhead::className(), ['TrNo' => 'TrNo']);
}

public function getGlfees(){
return $this->hasOne(Glfees::className(), ['TrNo' => 'TrNo']);
}

}?>


Glhead

<?php
namespace app\models;
use Yii;

use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

class Glhead extends ActiveRecord{
public static function tableName()
{
return '{{%glhead}}';
}


public function getScstock(){
return $this->hasOne(ActiveCurriculum::className(), ['TrNo' => 'TrNo']);
}

public function getSchead(){
return $this->hasOne(Schead::className(), ['TrNo' => 'TrNo']);
}

public function getGlfees(){
return $this->hasOne(Glfees::className(), ['TrNo' => 'TrNo']);
}

}?>


Glfees

<?php
namespace app\models;
use Yii;

use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

class Glfees extends ActiveRecord{
public static function tableName()
{
return '{{%glfees}}';
}

public function getGlhead(){
return $this->hasOne(Glhead::className(), ['TrNo' => 'TrNo']);
}

public function getSchead(){
return $this->hasOne(Schead::className(), ['TrNo' => 'TrNo']);
}


public function getScstock(){
return $this->hasOne(ActiveCurriculum::className(), ['TrNo' => 'TrNo']);
}




}?>


But I am getting this error when the query executes

enter image description here

Invalid Parameter – yii\base\InvalidParamException

app\models\ActiveCurriculum has no relation named "glhead".

Caused by: Unknown Method – yii\base\UnknownMethodException

Calling unknown method: app\models\ActiveCurriculum::getglhead()

in C:\xampp\htdocs\enrollment\vendor\yiisoft\yii2\base\Component.php at line 285


Am I doing something wrong with the relations or is it something else? Help would be greatly appreaciated. Thank you.

Answer

All these relations:

->joinWith('schead')
->joinWith('glhead')
->joinWith('glfees')

must be declared in ActiveCurriculum class. Consult relations in Yii2 documentation.