Reputation: 57
I have related tables (table train_schedule have departute_station_id and arrival_station_id) linked on Station: enter image description here
I want select records from train_schedule with stations name:
$trainsTchedule = TrainSchedule::find()
->joinWith('getArrivalStation')
->joinWith('getDepartuteStation()')
->all();
In TrainSchedule class:
/**
* @return \yii\db\ActiveQuery
*/
public function getArrivalStation()
{
return $this->hasOne(Station::className(), ['id' => 'arrival_station_id']);
}
And
/**
* @return \yii\db\ActiveQuery
*/
public function getDepartuteStation()
{
return $this->hasOne(Station::className(), ['id' => 'departute_station_id']);
}
Error:
Relation names are case sensitive. app\models\TrainSchedule has a relation named "arrivalStation" instead of "ArrivalStation".
Howe get data wrom linked tables?
Upvotes: 0
Views: 96
Reputation: 23738
You have defined your relations correctly but, you are calling them incorrectly. Your relation
getArrivalStation
should be specified as arrivalStation
getDepartuteStation()
should be departuteStation
when specifying in the joinWith
, and you need to provide an array
if you need to specify multiple relations when calling joinWith
as currently your second call ->joinWith('getDepartuteStation()')
is overriding the previous one ->joinWith('getArrivalStation')
.
So the query should look like below
$trainsTchedule = TrainSchedule::find()
->joinWith(['arrivalStation','departuteStation'])
->all();
You should read about Working with Relational Data
Upvotes: 3