这是我的complex (atleast i think it is complex)
条件,可以根据比赛时间表查找竞争对手并与事件相关。
现在我与HTBTM
表有events_competitors
个关系,其中多个事件有多个竞争对手用户条目。
在这里,我使用joins
条件加入并获得events
competitors
与is_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:
选择id
。Competitor
,first_name
。Competitor
,last_name
。Competitor
,parent_name
。{{1} },Competitor
。gender
,Competitor
。date_of_birth
,Competitor
。email_address
,Competitor
。weight
, Competitor
。weightgroup_id
,Competitor
。height
,Competitor
。rank_id
,Competitor
。degree_id
,{{ 1}}。Competitor
,photo
。Competitor
,school_id
。Competitor
,years_of_experience
。Competitor
,{{1} }。age
,Competitor
。tournament_id
,Competitor
。total_registration_fees
,Competitor
。address1
,Competitor
。 address2
,Competitor
。city
,Competitor
。zip_code
,Competitor
。country_id
,Competitor
。{{ 1}},state_id
。Competitor
,phone_number
。Competitor
,mobile_number
。Competitor
,payment_mode
。{{1} },Competitor
。email_sent
,Competitor
。payment_completed
,Competitor
。status
,Competitor
。created
, Competitor
。modified
,Rank
。id
,Rank
。name
,Rank
。status
,{{ 1}}。Rank
,created
。Rank
,modified
。Tournament
,id
。Tournament
,{{1} }。tournament_name
,Tournament
。tournament_type
,Tournament
。tournament_date
,Tournament
。venue_name
,Tournament
。 address1
,Tournament
。address2
,Tournament
。city
,Tournament
。zip_code
,Tournament
。{{ 1}},country_id
。Tournament
,state_id
。Tournament
,created
。Tournament
,modified
。{{1} },Country
。id
,Country
。name
,Country
。status
,Country
。created
, Country
。modified
,State
。id
,State
。country_id
,State
。name
,{{ 1}}。State
,short_name
。State
,status
。State
,created
。State
,{{1} }。modified
,Degree
。id
,Degree
。rank_id
,Degree
。name
,Degree
。 status
,Degree
。created
,School
。id
,School
。name
,School
。{{ 1}},address1
。School
,{{ 1}}。address2
,School
。city
,School
。zip_code
,School
。country_id
,{{1} }。School
,state_id
。School
,phone_number
。School
,owner_name
。School
,establishment_date
。 School
,total_competitors
。School
,status
。School
,created
。School
,modified
。{{ 1}},Transaction
。id
,Transaction
。competitor_id
,Transaction
。noncompetitor_id
,Transaction
。{{1} },created
。Transaction
,modified
。Transaction
,mc_gross
。Transaction
,address_status
。Transaction
, payer_id
。Transaction
,address_street
。Transaction
,payment_date
。Transaction
,payment_status
。Transaction
,{{ 1}}。address_zip
,Transaction
。first_name
,Transaction
。address_country_code
,Transaction
。address_name
,{{1} }。Transaction
,custom
。Transaction
,payer_status
。Transaction
,address_country
。Transaction
,address_city
。 Transaction
,payer_email
。Transaction
,verify_sign
。Transaction
,txn_id
。Transaction
,payment_type
。{{ 1}},Transaction
。last_name
,Transaction
。address_state
,Transaction
。receiver_email
FROM Transaction
AS {{1} }左JOIN event_competitors AS item_name
ON(Transaction
。mc_currency
= 3 AND Transaction
。item_number
= 0 AND Transaction
。{{1} } = 0)LEFT JOIN residence_country
AS Transaction
ON(transaction_subject
。Transaction
= payment_gross
。Transaction
)LEFT JOIN shipping
AS Transaction
ON(test_ipn
。Transaction
= pending_reason
。competitors
)LEFT JOIN Competitor
AS EventCompetitor
ON({{1} } {。{1}} = EventCompetitor
。event_id
)LEFT JOIN EventCompetitor
AS is_black
ON(EventCompetitor
。is_adult
= {{ 1}}。ranks
)LEFT JOIN Rank
AS Competitor
ON(rank_id
。Rank
= id
。tournaments
)LEFT加入Tournament
AS Competitor
ON(tournament_id
。Tournament
= id
。countries
)LEFT JOIN Country
AS Competitor
ON(country_id
。Country
= id
。states
)WHERE State
。Competitor
= 1 AND {{1 } {。{1}} = 1 AND state_id
。State
= 13 AND id
。degrees
= 11 AND Degree
。Competitor
='0'和degree_id
。Degree
='女'GROUP BY id
。schools
以下是来自ref:
的查询的左连接条件 School
答案 0 :(得分:0)
您应该使用可包含的行为。更多信息:http://book.cakephp.org/view/1323/Containable
var $actsAs = array('Containable');
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参数传递它们。