我有“users(id,name)”和“userdrivermapping(id,user_id,driver_id)”表。 user_id和driver_id都是来自users表的id的外键。我想在用户视图页面中显示驱动程序名称。我不知道如何在表关联中选择用户字段。 “名称字段始终返回空”
这是我的控制器代码
$id = 7 ;
$query = $this->Users->UserDriverMapping->find()
->contain([
'Users' => function ($q) use($id) {
return $q
->select(['UserDriverMapping.id,user_id, driver_id,name'])
->where(['UserDriverMapping.user_id' => $id]);
}
]);
foreach ($query as $driver) {
echo $driverId = $driver->user_id;
echo $driverId = $driver->name;
}