cakephp3-无法显示值的计数
我试图显示房间中的用户数量。但当我尝试这样做时,我会出错 这就是关系cakephp3-无法显示值的计数,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,我试图显示房间中的用户数量。但当我尝试这样做时,我会出错 这就是关系 Rooms has many users. Rooms belong to groups. public function initialize(array $config) { parent::initialize($config); $this->table('rooms'); $this->displayField('name'); $this->primaryKey
Rooms has many users.
Rooms belong to groups.
public function initialize(array $config)
{
parent::initialize($config);
$this->table('rooms');
$this->displayField('name');
$this->primaryKey('id');
$this->belongsTo('Groups', [
'foreignKey' => 'group_id'
]);
$this->hasMany('Users', [
'foreignKey' => 'room_id'
]);
}
为了实现这一点,我编写了一个控制器函数,如下所示:
public function index()
{
$this->loadComponent('Prg');
$this->Prg->commonProcess();
$params = $this->Prg->parsedParams();
$this->paginate = [
'limit' => 25,
'order' => ['id' => 'ASC'],
'finder' => ['RoomsList' =>['filter'=> $params]],
'sortWhitelist' => ['Rooms.id','Rooms.name'],
'extraOptions' =>['params' => $params]
];
$rooms = $this->paginate($this->Rooms);
$this->set(compact('rooms'));
$this->set('_serialize', ['rooms']);
}
public function findRoomsList(Query $query, array $options)
{
$query->select(['id','name','modified','created'])
->contain([
'Groups'=> function ($query) {
return $query->select(['id','name']);
},
'Users' => function($query){
return $query->select(['status','full_name']);
//want to fetch total number of users
}
]);
echo $query;
return $query;
}
此功能可为可用房间列表分页。这很好,但是现在当我试图显示每个房间中的用户数时,它抛出了错误
我修改了我的模型函数,如下所示:
public function index()
{
$this->loadComponent('Prg');
$this->Prg->commonProcess();
$params = $this->Prg->parsedParams();
$this->paginate = [
'limit' => 25,
'order' => ['id' => 'ASC'],
'finder' => ['RoomsList' =>['filter'=> $params]],
'sortWhitelist' => ['Rooms.id','Rooms.name'],
'extraOptions' =>['params' => $params]
];
$rooms = $this->paginate($this->Rooms);
$this->set(compact('rooms'));
$this->set('_serialize', ['rooms']);
}
public function findRoomsList(Query $query, array $options)
{
$query->select(['id','name','modified','created'])
->contain([
'Groups'=> function ($query) {
return $query->select(['id','name']);
},
'Users' => function($query){
return $query->select(['status','full_name']);
//want to fetch total number of users
}
]);
echo $query;
return $query;
}
现在我得到了这个错误:
You are required to select the "Users.room_id" field(s)
有人能告诉我我在这里犯的错误吗?我是CakePHP3新手。您是否尝试过在用户容器中选择的字段列表中包含房间id?