yii2: relation eager loading not working
What steps will reproduce the problem?
There is 3 entity
- Service - some Soft installed on VM (may be instaleld on many VMs for clustered services)
- Comp - VM which running Service
- Arm - Physical PC/Laptop which runs VMs (may run many) which runs services
Tables is like services:
- id
comps_in_services:
- id
- comps_id
- services_id
os:
- id
- arms_id
arms:
- id
so i wish to know which physical PCs involved in clustered service stability:
class Services extends \yii\db\ActiveRecord
{
...
public function getComps()
{
return static::hasMany(Comps::class, ['id' => 'comps_id'])
->viaTable('{{%comps_in_services}}', ['services_id' => 'id']);
}
public function getArms()
{
return static::hasMany(Arms::class, ['id' => 'arm_id'])
->via('comps');
}
...
}
class ServicesSearch extends Services
{
....
public function search($params)
{
$query = Services::find()
->joinWith([
'arms', //here is the bug comes!!!
]);
$this->load($params); //filter nothing
return new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);
}
$searchModel = new ServicesSearch();
$dataProvider = $searchModel->search([]);
var_dump($dataProvider->query->createCommand()->getRawSql());
var_dump($dataProvider->models[3]);
What is the expected result?
same result on accessing arms field on services objects with or without eager loading of arms relation
What do you get instead?
all works nice without usage of joinWith or with eagerload=false set but if use joinWith (with eagerload enabled by default) or use “with” - some sevices loaded with empty arms field
dumped Query is same and correct in both cases but loaded ActiveRecords objects differs (WTF???)
Without EagerLoading (columns Service,Comp,Arm):
With EagerLoading:
Additional info
Q | A |
---|---|
Yii version | 2.0.46 |
PHP version | 7.3 |
Operating system | Windows |
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 21 (20 by maintainers)
Commits related to this issue
- Update ActiveRelationTrait.php fix #19507 — committed to spo0okie/yii2 by spo0okie 2 years ago
- fix #19507 pass ActiveRecordTest::testJoinWithEager — committed to spo0okie/yii2 by spo0okie 2 years ago
- fix #19507 pass ActiveRecordTest::testJoinWithEager — committed to spo0okie/yii2 by spo0okie 2 years ago
- Fix #19507: Fix eager loading of nested relations — committed to spo0okie/yii2 by spo0okie 2 years ago
- Fix eager loading of nested one-to-many relations (#19585) * Update ActiveRelationTrait.php fix #19507 * update structure-applications.md wiki links (#19597) * update structure-models.md wik... — committed to yiisoft/yii2 by spo0okie 2 years ago
Ok it is clear now. I prepare a test.