cakePHP表联接两个表的问题 [英] cakePHP table joining two tables issue

查看:78
本文介绍了cakePHP表联接两个表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cakePHP是我的新手,整个表的关系概念非常令人困惑!

Im new to cakePHP and the whole table relations concept is very confusing!

我有2个表,能力和权限。能力存储具有ID的名称列表。

I have 2 tables, competencies and competenceRatings. competencies stores a list of names with ids.

competencies
------------
id
name

用户可以从此表中选择各种能力并对它们进行评分,并将他们的评分存储到competenceRatings表中

And users can select various competencies from this table and rate them and their ratings are stored into competenceRatings table.

competenceRatings
-----------------
id
competence_id
user_id
rating

我想成为能够获取用户尚未对能力等级表进行任何评分的能力的名称。即,我需要从competencyRatings表中没有任何条目的表(对于给定的user_id)。

I want to be able to get the names of competencies for which a user have NOT made any ratings into competenceRatings table. i.e., I need list of names from competencies table for which there are no entries in comptenceRatings table(for given user_id).

我尝试了功能-> hasMany-> competenceRatings,

I tried competencies->hasMany->competenceRatings, competenceRatings->belongsTo->competencies relations.

$competencies = $this->Competence->CompetenceRating->find('all',array('CompetenceRating.user_id' => $userId,'CompetenceRating.competence_id !=' => 'Competence.id'));

但是没有用!

这样做结果需要其他关系吗?还是可以在查找查询中使用联接条件联接表?

Does this result require any other relations? Or can i just join tables using joins condition in find query?

谢谢。

编辑

此方法有效:

$options['joins'] = array(
    array(
        'table' => 'competence_ratings',
        'alias' => 'CompetenceRating',
        'type' => 'LEFT OUTER',
        'conditions' => array(
            'Competence.id = CompetenceRating.competence_id',
            'CompetenceRating.user_id' => $userId
        )
    )
);
$options['conditions'] = array( 'CompetenceRating.competence_id'=> null );


推荐答案

尝试一下

联接表

$data = $this->Competence->find('all', array('joins' => array(
    array(
        'table' => 'competenceRatings',
        'alias' => 'CompetenceRating',
        'type' => 'inner',
        'foreignKey' => false,
        'conditions'=> array('CompetenceRating.competencie_id = Competence.id')
    ),
    array(
        'table' => 'competencies',
        'alias' => 'Competence',
        'type' => 'inner',
        'foreignKey' => false,
        'conditions'=> array(
            'Competence.id = MarkersTag.tag_id',
            'Competence.user_id' => $user_id
        )
    )
)));

这篇关于cakePHP表联接两个表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆