我在User和Item之间有一个HasMany-Through关系。问题是,我想检查关系是否存在(意味着在同一行中准确查找user_id和item_id),以检查所采取的操作是添加还是删除该关系。
问题似乎出在这行代码中,无论我以什么用户的身份登录,它的计算结果总是大于0,而如果我以没有投票权的用户身份登录,则它应该什么都找不到:
$something = $this->Upvote->find('count', array('Upvote.item_id' => $this->request->data['item_id'], 'Upvote.user_id' => AuthComponent::user('id')));
基本上,我想做的就是找出登录用户和每个项目之间是否存在HasMany-Through关系。我该怎么做?
试试这个
$something = $this->Upvote->find('count',
array( 'conditions'=>
array('Upvote.item_id' => $this->request->data['item_id'],
'Upvote.user_id' => AuthComponent::user('id')
)
)
);
u2460470的代码是正确的。您需要在项目之间循环:
foreach ($items as $item){
$upvotes = $this->request->find('count',array('conditions'=>
array('Upvote.item_id' => $item,
'Upvote.user_id' => AuthComponent::user('id')
)));}