CakePHP多个JOIN findAll条件问题

时间:2011-04-08 06:25:56

标签: cakephp join conditional-statements findall

这是我的complex (atleast i think it is complex)条件,可以根据比赛时间表查找竞争对手并与事件相关。

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

在这里,我使用joins条件加入并获得events competitorsis_black相关的工作正常,但我还想为is_adult应用其他条件(检查黑带)和'EventCompetitor.is_black' => 0, 'EventCompetitor.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'

                                  )

                             );

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

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

JOIN

任何想法,如何将这些内容应用到Competitor条件中,以便将其应用到结果中。

谢谢!

以下是您的参考的SQL Dump:

选择idCompetitorfirst_nameCompetitorlast_nameCompetitorparent_name。{{1} },CompetitorgenderCompetitordate_of_birthCompetitoremail_addressCompetitorweightCompetitorweightgroup_idCompetitorheightCompetitorrank_idCompetitordegree_id,{{ 1}}。CompetitorphotoCompetitorschool_idCompetitoryears_of_experienceCompetitor,{{1} }。ageCompetitortournament_idCompetitortotal_registration_feesCompetitoraddress1Competitoraddress2CompetitorcityCompetitorzip_codeCompetitorcountry_idCompetitor。{{ 1}},state_idCompetitorphone_numberCompetitormobile_numberCompetitorpayment_mode。{{1} },Competitoremail_sentCompetitorpayment_completedCompetitorstatusCompetitorcreatedCompetitormodifiedRankidRanknameRankstatus,{{ 1}}。RankcreatedRankmodifiedTournamentidTournament,{{1} }。tournament_nameTournamenttournament_typeTournamenttournament_dateTournamentvenue_nameTournamentaddress1Tournamentaddress2TournamentcityTournamentzip_codeTournament。{{ 1}},country_idTournamentstate_idTournamentcreatedTournamentmodified。{{1} },CountryidCountrynameCountrystatusCountrycreatedCountrymodifiedStateidStatecountry_idStatename,{{ 1}}。Stateshort_nameStatestatusStatecreatedState,{{1} }。modifiedDegreeidDegreerank_idDegreenameDegreestatusDegreecreatedSchoolidSchoolnameSchool。{{ 1}},address1School,{{ 1}}。address2SchoolcitySchoolzip_codeSchoolcountry_id,{{1} }。Schoolstate_idSchoolphone_numberSchoolowner_nameSchoolestablishment_dateSchooltotal_competitorsSchoolstatusSchoolcreatedSchoolmodified。{{ 1}},TransactionidTransactioncompetitor_idTransactionnoncompetitor_idTransaction。{{1} },createdTransactionmodifiedTransactionmc_grossTransactionaddress_statusTransactionpayer_idTransactionaddress_streetTransactionpayment_dateTransactionpayment_statusTransaction,{{ 1}}。address_zipTransactionfirst_nameTransactionaddress_country_codeTransactionaddress_name,{{1} }。TransactioncustomTransactionpayer_statusTransactionaddress_countryTransactionaddress_cityTransactionpayer_emailTransactionverify_signTransactiontxn_idTransactionpayment_type。{{ 1}},Transactionlast_nameTransactionaddress_stateTransactionreceiver_email FROM Transaction AS {{1} }左JOIN event_competitors AS item_name ON(Transactionmc_currency = 3 AND Transactionitem_number = 0 AND Transaction。{{1} } = 0)LEFT JOIN residence_country AS Transaction ON(transaction_subjectTransaction = payment_grossTransaction)LEFT JOIN shipping AS Transaction ON(test_ipnTransaction = pending_reasoncompetitors)LEFT JOIN Competitor AS EventCompetitor ON({{1} } {。{1}} = EventCompetitorevent_id)LEFT JOIN EventCompetitor AS is_black ON(EventCompetitoris_adult = {{ 1}}。ranks)LEFT JOIN Rank AS Competitor ON(rank_idRank = idtournaments)LEFT加入Tournament AS Competitor ON(tournament_idTournament = idcountries)LEFT JOIN Country AS Competitor ON(country_idCountry = idstates)WHERE StateCompetitor = 1 AND {{1 } {。{1}} = 1 AND state_idState = 13 AND iddegrees = 11 AND DegreeCompetitor ='0'和degree_idDegree ='女'GROUP BY idschools

以下是来自ref:

的查询的左连接条件

School

1 个答案:

答案 0 :(得分:0)

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

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

      3)要注入事件id,请将contains数组传递给find操作:

      $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,那么您可能希望从模型中移除这些条件,并根据需要通过find操作的contains参数传递它们。