select('tlog.tmc, ttmc.title, ts.title as status, ta.name, t.employee, tlog.issue_date') ->from(\app\models\entity\UserDevice::tableName() . ' t') ->leftJoin(\app\models\entity\Log::tableName() . ' tlog', 'tlog.id = t.phone or tlog.id = t.camera or tlog.id = t.oil_meter') ->leftJoin(\app\models\entity\Tmc::tableName() . ' ttmc', 'ttmc.id = tlog.tmc') ->leftJoin(\app\models\entity\TmcStatus::tableName() . ' ts', 'ts.id = ttmc.status') ->leftJoin(\app\models\entity\Accounts::tableName() . ' ta', 'ta.id = t.employee') ; $userDevices = $query->all(); $usersWithDevice = []; foreach ($userDevices as $userDevice) { $usersWithDevice[$userDevice['employee']][] = $userDevice; } ?>