phpyii2

Yii2 model relationship where not null


I would like to get all records in a relationship where its not null

so my table are

tbl_truck
    id, name

 tbl_checks
   id
   truck_id //foreign key from tbl_truck table id

So in my query i have

$query = TblTrucksModel::find()
         ->leftJoin('tbl_checks','tbl_trucks.id = tbl_checks.truck_id')
         ->where() //here add the condition

So basically i would like to fetch only the id's from tbl_truck which are also existent in tbl_checks

Nb: TblTrucksModel represents the tbl_trucks table

How do i go on about this.


Solution

  • Should be using the operator syntax:

        $query = TblTrucksModel::find()
         ->leftJoin('tbl_checks','tbl_trucks.id = tbl_checks.truck_id')
         ->where(['not', ['tbl_trucks.id' => null]])