在 CakePHP 3 中过滤可包含的
filter on containable in CakePHP 3
我正在使用 CakePHP 3.4+
我有以下表格及其关联。
PostVideos->belongsTo(Campaigns) || PostVideos 有列 campaign_id
活动->hasMany(ClientCampaigns) || ClientCampaigns 有列 campaign_id
UserClients->hasMany(ClientCampaigns) || ClientCampaigns 有 user_client_id
UserClients 有列 client_id
我想 select/count 在 PostVideos 上 client_id 在 UserClients
$count['videos']['uploaded'] = $this->Campaigns->PostVideos->find()
->where(['UserClients.client_id' => $this->Auth->user('id')])
->contain(['Campaigns.ClientCampaigns.UserClients'])
->count();
但它给出数据库错误为
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'UserClients.client_id' in 'where clause'
为此你必须使用 matching
$userId=$this-Auth->user('id');
$this->Campaigns->PostVideos->find()
->matching('Campaings.ClientCampaings.UserClients',function(Query $query) use ($userId) {
return $query->where(['UserClients.client_id' => $userId])
}
->contain(['Campaigns.ClientCampaigns.UserClients'])
->count();
如果不需要加载UserClients
可以跳过contain
。
我正在使用 CakePHP 3.4+
我有以下表格及其关联。
PostVideos->belongsTo(Campaigns) || PostVideos 有列 campaign_id
活动->hasMany(ClientCampaigns) || ClientCampaigns 有列 campaign_id
UserClients->hasMany(ClientCampaigns) || ClientCampaigns 有 user_client_id
UserClients 有列 client_id
我想 select/count 在 PostVideos 上 client_id 在 UserClients
$count['videos']['uploaded'] = $this->Campaigns->PostVideos->find()
->where(['UserClients.client_id' => $this->Auth->user('id')])
->contain(['Campaigns.ClientCampaigns.UserClients'])
->count();
但它给出数据库错误为
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'UserClients.client_id' in 'where clause'
为此你必须使用 matching
$userId=$this-Auth->user('id');
$this->Campaigns->PostVideos->find()
->matching('Campaings.ClientCampaings.UserClients',function(Query $query) use ($userId) {
return $query->where(['UserClients.client_id' => $userId])
}
->contain(['Campaigns.ClientCampaigns.UserClients'])
->count();
如果不需要加载UserClients
可以跳过contain
。