Используйте
Gii генератор и он все сделает за вас.
Модель
Department.
class Department extends \yii\db\ActiveRecord {
/**
* @return \yii\db\ActiveQuery
*/
public function getBuildings() {
return $this->hasMany(Building::className(), ['department_id' => 'id']);
}
// ...
}
Может получать:
var_dump($department->buildings);
Модель
Building.
class Building extends \yii\db\ActiveRecord {
/**
* @return \yii\db\ActiveQuery
*/
public function getDepartment() {
return $this->hasOne(Department::className(), ['id' => 'department_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getRooms() {
return $this->hasMany(Room::className(), ['building_id' => 'id']);
}
// ...
}
Может получать:
var_dump($building->department);
var_dump($building->rooms);
Модель
Room.
class Room extends \yii\db\ActiveRecord {
/**
* @return \yii\db\ActiveQuery
*/
public function getBuilding() {
return $this->hasOne(Building::className(), ['id' => 'building_id']);
}
// ...
}
Может получать:
var_dump($room->building);