all! I hope anyone knows how to find a solution for this problem. I've 3 tables for joining, but when I try to make joining via join method I cannot get related tables and where statement isn't working at all. So, I've a method
public static function get_by_abc($letter = null,
$object = false) {
$result = self::find()
->join('INNER JOIN', 'geo_place', 'geo_place.city_id = geo_city.id')
->join('INNER JOIN', 'bus_station', 'geo_place.place_id = bus_station.geo_place_id')
->join('INNER JOIN', 'bus_company', 'bus_station.company_id = bus_company.id')
//->innerJoinWith('companies')
->where(['bus_company.active' => 1])
->andWhere(['like', 'geo_city.name', $letter.'%', false]);
if(!$object) $result->asArray();
return $result->all(); //->createCommand()->rawSql
}
Next, I was trying to make a solution via with yii2 method, however It wasn't working too :( I looks like
public function getCompanies() {
return $this->hasMany(BusCompany::className(), ['id' => 'company_id'])
->viaTable('bus_station', ['geo_place_id' => 'place_id'])
->viaTable('geo_place', ['city_id' => 'id']);
}
But in this case I have join with geo_place and bus_company, but not with bus_station.
So a structure of the tables looks like this:
Try to define relation tree like this:
public function getBusStation()
{
return $this->hasOne(BusStation::className(), [...]);
}
public function getBusCompanies()
{
return $this->hasMany(BusCompany::className(), ['id' => 'company_id'])->via('busStation'); // it will use your relation `getBusStation()` to join bus companies by this relation
}
Its easy to maintain and understand, hope it will help You.