1
votes

I have setup my dataprovider with left join as

$trucks = TblTrucks::find()
                  ->leftJoin("tbl_checks", "tbl_checks.truck_id=tbl_trucks.id")
                  ->Where(["town"=>$town])

                  ->andWhere(["truck_status"=>6])
                  ->andWhere(["between","tbl_trucks.created_at", $ts1, $ts2 ]);


    $provider = new ActiveDataProvider([
        'query' => $trucks,
        'pagination' =>false,
    ]);

Now am getting thje data via

return $provider->getModels();

This returns only tbl_trucks data how can i still get the data in the tbl_checks

1
I think you can get data you need from relations specified in your ActiveRecord classes.ArtOsi
how specifically since getModel() doesntGeoff
I'm not sure but I think you can call for example $model->getChecks() on each model you get from $provider->getModels() assuming you have getChecks() method which returns related records in your TblTrucks classArtOsi
add tblTrucks model.Insane Skull

1 Answers

0
votes

To get related field in model first of all your must have relation model TblChecks define relation in model TblTrucks acording to documentation In you case it will be some like that:

public function getTblChecks()
{
    return $this->hasOne(TblChecks::className(), ['truck_id' => 'id']);
    //or, depending on the type of relation (on-to-one or one-to-maty) 
    return $this->hasMany(TblChecks::className(), ['truck_id' => 'id']);
}

Than use method joinWith:

$trucks = TblTrucks::find()
              ->joinWith(['tblChecks'])
              ->Where(["town"=>$town])
              ->andWhere(["truck_status"=>6])
              ->andWhere(["between","tbl_trucks.created_at", $ts1, $ts2 ]);

And then you can get relation field just call it:

$models = $provider->getModels();
$models[index]->tblChecks->needed_field

But if you need just array of modesl acording to your query you dont have to use ActiveDataProvider just call method all() For example:

$trucks = TblTrucks::find()
              ->joinWith(['tblChecks'])
              ->Where(["town"=>$town])
              ->andWhere(["truck_status"=>6])
              ->andWhere(["between","tbl_trucks.created_at", $ts1, $ts2 ])->all();

And in var $trucks you will have array of models TblTrucks with relation models (TblChecks in this case) acording to your query.