1

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

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

在这里,我使用joins了条件来加入和建立相关关系eventscompetitors哪个工作正常,但我还想申请附加条件,用于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 转储:

选择Competitorid, Competitor. first_name, Competitor. last_name, Competitor. parent_name, Competitor. gender, Competitor. date_of_birth, Competitor. email_address, Competitor. weight, Competitor. weightgroup_id, Competitor. height, Competitor. rank_id, Competitor. degree_id, Competitor. photo, Competitor. school_id, Competitor. years_of_experience, Competitor. age, Competitor. tournament_id, Competitor. total_registration_fees, Competitor. address1, Competitor. address2, Competitor. city, Competitor. zip_code, Competitor. country_id, Competitor. state_id, Competitor. phone_number, Competitor. mobile_number, Competitor. payment_mode, Competitor. email_sent, Competitor.payment_completed, Competitor. status, Competitor. created, Competitor. modified, Rank. id, Rank. name, Rank. status, Rank. created, Rank. modified, Tournament. id, Tournament. tournament_name, Tournament. tournament_type, Tournament. tournament_date, Tournament. venue_name, Tournament. address1, Tournament. address2, Tournament. city, Tournament. zip_code, Tournament. country_id, Tournament. state_id, Tournament. created, Tournament. modified, Country. id, Country. name, Country. status, Country. created, Country. modified, State. id, State. country_id, State. name,State. short_name, State. status, State. created, State. modified, Degree. id, Degree. rank_id, Degree. name, Degree. status, Degree. created, School. id, School. name, School. address1, School. address2, School. city, School. zip_code, School. country_id, School. state_id, School. phone_number, School. owner_name, School. establishment_date, School. total_competitors, School. status, School. created, School. modified, Transaction. id, Transaction. competitor_id, Transaction. noncompetitor_id, Transaction. created, Transaction. modified, Transaction.mc_gross, Transaction. address_status, Transaction. payer_id, Transaction. address_street, Transaction. payment_date, Transaction. payment_status, Transaction. address_zip, Transaction. first_name, Transaction. address_country_code, Transaction. address_name, Transaction. custom, Transaction. payer_status, Transaction. address_country, Transaction. address_city, Transaction. payer_email, Transaction. verify_sign, Transaction. txn_id, Transaction. payment_type, Transaction. last_name, Transaction. address_state, Transaction. receiver_email, Transaction. item_name, Transaction. mc_currency, Transaction. item_number, Transaction. residence_country, Transaction. transaction_subject, Transaction. payment_gross, Transaction. shipping, Transaction. test_ipn, Transaction. pending_reasoncompetitorsASCompetitor左 JOIN event_competitors AS EventCompetitorON ( EventCompetitor. event_id= 3 AND EventCompetitor. is_black= 0 AND EventCompetitor. is_adult= 0) LEFT JOIN ranksAS RankON ( Competitor. rank_id= Rank. id) LEFT JOIN tournamentsAS TournamentON ( Competitor. tournament_id= Tournament. id) LEFT JOIN countriesAS CountryON ( Competitor. country_id= Country. id) LEFT JOIN statesAS StateON ( Competitor. state_id= State. id) 左连接degrees打开Degree( Competitor. degree_id= Degree. id) 左连接schools打开School( Competitor. school_id= School. id) 左连接transactions打开Transaction(Transaction. competitor_id= Competitorid) 在哪里Competitorstatus= 1 和Competitorpayment_completed= 1 和Competitorweightgroup_id= 13 和Competitorrank_id= 11 和Competitordegree_id=“0”和Competitorgender=“女性”分组Competitorid

以下是上述 ref 查询的左连接条件:

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

4

1 回答 1

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

于 2011-04-08T18:23:01.160 回答