1
votes

Yii Framework ist really great, but I encountered a problem when using Active Record for querying data from MySQL database.

When I join 2 tables ('building' and 'building_info') in my Function "get" in model Building, there will be no data returned from my second table. If I execute the same query with Query Class, rows from both table will be returned. With Active Record I get only data from table 'building'.

Model Building:

...
// Setting Relation
public function getBuildingInfos()
{
    return $this->hasMany(BuildingInfo::className(), ['BuildingID' => 'ID']);
}    

// Get all buildings
public function get() {
    $building = Building::find()
        ->joinWith('buildingInfos')
        ->where(['building_info.langID' => 1])
        ->all();

    return $building;
}
// Attributes
public function attributeLabels()
{
    return [
        'ID' => 'ID',
        'NameBreak' => 'Name Break',
        'Tileset' => 'Tileset',
        'TilesetPosition' => 'Tileset Position',
        ...
    ]
}
...

Model BuildingInfo:

...
public function attributeLabels()
{
    return [
        'buInfoID' => 'Bu Info ID',
        'BuildingID' => 'Building ID',
        'langID' => 'Lang ID',
        'Name' => 'Name',
        'ShortDesc' => 'Short Desc',
        'ShortDescDisabled' => 'Short Desc Disabled',
    ];
} 
public function getBuilding()
{
    return $this->hasOne(Building::className(), ['ID' => 'BuildingID']);
}
...

Do you know how to solve this problem?

Thanks Kevin

1

1 Answers

0
votes

Ciao Kevin :) are you using yii1 or yii2?

In 'getBuildingInfos', the table is linked by BuidingID, in your get function, you are linking it by building_info.langID.. Have you already verified that your join works in sql?