蛋糕PHP多个JOIN查找所有条件问题



这是我从比赛时间表和与事件相关的比赛中寻找竞争对手complex (atleast i think it is complex)条件。

现在我与events_competitors表有HTBTM关系,其中多个事件有多个竞争对手用户条目。

在这里,我使用了joins条件来加入和获得相关events competitors工作正常,但我还想应用其他条件,用于is_black(检查黑带(和is_adult(检查成年人(

'EventCompetitor.is_black' => 0,
'EventCompetitor.is_adult' => 0,

在这里,我只希望那些同时具有两个条件(is_black/is_adult(的竞争对手为0,意味着不符合条件,但它不适用相同,这会导致错误的竞争对手结果。

以下是我的完整查找条件:

$matchdivisions = $this->Competitor->find("all" , 
                               array(
                                'conditions' => 
                                   array(
                   'Competitor.status' => 1, 
                   'Competitor.payment_completed' => 1,
                   'Competitor.weightgroup_id' => $current_matchsc['Matchschedule']['weightgroup_id'],
                   'Competitor.rank_id' => $current_matchsc['Matchschedule']['rank_id'],
                   'Competitor.degree_id' => $current_matchsc['Matchschedule']['degree_id'],
                   'Competitor.gender' => $current_matchsc['Matchschedule']['gender'],
                                    ),
                                    'joins' => 
                                      array(
                                        array(
                                           'table' => 'event_competitors',
                                           'alias' => 'EventCompetitor',
                                           'type' => 'left',
                                           'conditions'=> array(
                                                "AND" =>array(
                        'EventCompetitor.event_id = '.$current_matchsc['Event']['id'],
                        'EventCompetitor.is_black' => 0,
                        'EventCompetitor.is_adult' => 0,
                                                      )
                                                 ),
                                           )
                                     ),
                                    'group' => 'Competitor.id'
                                  )
                             );

任何想法,我怎样才能将这些东西应用到JOIN条件下,所以它被应用到结果中。

谢谢!

下面是您的参考的 SQL 转储:

选择CompetitoridCompetitor .first_nameCompetitorlast_nameCompetitor .parent_nameCompetitor .genderCompetitor .date_of_birthCompetitor .email_addressCompetitor .weightCompetitorweightgroup_idCompetitor .heightCompetitorrank_idCompetitordegree_idCompetitor .photoCompetitorschool_idCompetitoryears_of_experienceCompetitorageCompetitor .tournament_idCompetitor .total_registration_feesCompetitoraddress1Competitoraddress2Competitor .cityCompetitor .zip_codeCompetitorcountry_idCompetitorstate_idCompetitorphone_numberCompetitormobile_numberCompetitor .payment_modeCompetitor .email_sentCompetitor .payment_completedCompetitor .statusCompetitorcreatedCompetitor .modifiedRank .idRanknameRank .statusRankcreatedRankmodifiedTournament .idTournamenttournament_nameTournamenttournament_typeTournament .tournament_dateTournament .venue_nameTournament .address1Tournament .address2TournamentcityTournamentzip_codeTournamentcountry_idTournamentstate_idTournamentcreatedTournament .modifiedCountry .idCountrynameCountrystatusCountrycreatedCountrymodifiedState .idStatecountry_idStatenameStateshort_nameState .statusStatecreatedState .modifiedDegreeidDegree .rank_idDegreenameDegree .statusDegree .createdSchoolidSchoolnameSchooladdress1School .address2SchoolcitySchool .zip_codeSchoolcountry_idSchoolstate_idSchoolphone_numberSchoolowner_nameSchoolestablishment_dateSchooltotal_competitorsSchool .statusSchool .createdSchoolmodifiedTransaction .idTransactioncompetitor_idTransaction .noncompetitor_idTransactioncreatedTransactionmodifiedTransactionmc_grossTransaction .address_statusTransactionpayer_idTransaction .address_streetTransactionpayment_dateTransactionpayment_statusTransactionaddress_zipTransactionfirst_nameTransaction .address_country_codeTransaction .address_nameTransactioncustomTransactionpayer_statusTransactionaddress_countryTransactionaddress_cityTransaction .payer_emailTransactionverify_signTransactiontxn_idTransaction .payment_typeTransaction .last_nameTransactionaddress_stateTransaction .receiver_emailTransactionitem_nameTransactionmc_currencyTransactionitem_numberTransactionresidence_countryTransactiontransaction_subjectTransactionpayment_grossTransaction .shippingTransactiontest_ipnTransactionpending_reasoncompetitors作为Competitor左加入event_competitors作为EventCompetitor(EventCompetitorevent_id = 3 和 EventCompetitor .is_black = 0 和 EventCompetitor .is_adult = 0( 左联接ranksRank ( Competitor .rank_id = Rank .id ( 左加入tournaments,如Tournament 上 ( Competitor .tournament_id = Tournament .id ( 左加入countries作为Country ( Competitor .country_id = Country .id ( 左加入states作为State ( Competitor .state_id = State .id ( 左加入degrees作为Degree ( Competitor .degree_id = Degree .id ( 左加入schools作为School ( Competitor .school_id = School .id ( 左加入transactions作为Transaction ( Transaction .competitor_id = Competitor .id ( Competitor .status = 1 和 Competitor .payment_completed = 1 和 Competitor .weightgroup_id = 13 和 Competitor .rank_id = 11 和 Competitor .degree_id = '0' 和 Competitor .gender = "女性"分组按Competitor .id

这是上面查询中 ref 的左连接条件:

left JOIN event_competitors AS EventCompetitor ON (EventCompetitor.event_id = 3 AND EventCompetitor.is_black = 0 AND EventCompetitor.is_adult = 0)

您应该为此使用可包含的行为。 更多内容: http://book.cakephp.org/view/1323/Containable

  1. 将其添加到您的竞争对手模型中。

var $actsAs = array('Containable');
  1. 更新竞争对手模型中的模型关系,以包含is_black和is_adult条件:
    
var $hasAndBelongsToMany = array(
                'Competitor' => array(
                    'className' => 'Competitor',
                    'joinTable' => 'event_competitors',
                    'alias' => 'EventCompetitor',
                    'conditions' => array(
                        'EventCompetitor.is_black' => 0,
                        'EventCompetitor.is_adult' => 0
                    )
                 )
            );

3( 要注入事件 ID,请将包含数组传递给查找操作:

$contain = array(
    'EventCompetitor' => array(
        'conditions' => array('EventCompetitor.event_id' =>  $current_matchsc['Event']['id'])
    )
);
$matchdivisions = $this->Competitor->find("all" , 
    array(
        'contain' => $contain,
        'conditions' => array(
            'Competitor.status' => 1, 
             'Competitor.payment_completed' => 1,
            'Competitor.weightgroup_id' => $current_matchsc['Matchschedule']['weightgroup_id'],
            'Competitor.rank_id' => $current_matchsc['Matchschedule']['rank_id'],
            'Competitor.degree_id' => $current_matchsc['Matchschedule']['degree_id'],
            'Competitor.gender' => $current_matchsc['Matchschedule']['gender']
        )
    )
);

如果关系并不总是需要is_black和is_adult,则需要将这些条件从模型中移出,并根据需要通过查找操作的 include 参数传入。

相关内容

  • 没有找到相关文章

最新更新