CakePHP多重JOIN findAll条件问题
这是我的复杂(至少我认为很复杂)
条件,用于从比赛日程和与赛事相关的内容中查找竞争对手。
现在我与 events_competitors
表建立了 HTBTM
关系,其中多个事件有多个竞争对手用户条目。
在这里,我使用了 joins
条件来加入和获取与 competitors
相关的 event
,效果很好,但我还想应用其他条件,例如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 转储:
SELECT Competitor
.id
, Competitor
.first_name
, Competitor< /code>.
last_name
、竞争对手
.parent_name
、竞争对手
.性别
、 竞争对手
.出生日期
、竞争对手
.电子邮件地址
、竞争对手
.体重< /code>,
竞争对手
.weightgroup_id
, 竞争对手
.身高
, 竞争对手
.rank_id
、竞争对手
.level_id
、竞争对手
.照片< /code>,
竞争对手
.school_id
, 竞争对手
.years_of_experience
, 参赛者
.年龄
、参赛者
.tournament_id
、参赛者
.total_registration_fees< /code>,
竞争对手
.地址1
, 竞争对手
.地址2
, 竞争对手
.城市
、竞争对手
.zip_code
、竞争对手
.country_id< /code>,
竞争对手
.state_id
, 竞争对手
.电话号码
, 竞争对手
.手机号码
、竞争对手
.payment_mode
、竞争对手
.email_sent< /code>,
竞争对手
.付款完成
, 竞争对手
.状态
, 竞争对手
.创建
、竞争对手
.修改
、排名
.id< /code>,
排名
.名称
, 排名
.状态
, 排名
.<代码>创建, 排名
.修改
、锦标赛
.id
、锦标赛
.tournament_name< /code>,
锦标赛
.tournament_type
, 锦标赛
.tournament_date
, 锦标赛
.venue_name
、锦标赛
.address1
、锦标赛
.address2< /code>,
锦标赛
.城市
, 锦标赛
.zip_code
, 锦标赛
.country_id
、锦标赛
.state_id
、锦标赛
.已创建< /code>,
锦标赛
.修改
, 国家/地区
.id
, 国家/地区
.<代码>名称, 国家/地区
.状态
、国家/地区
.已创建
、国家/地区
.已修改< /code>,
州
.id
, 州
.country_id
, 州
.<代码>名称, 状态
.短名称
、状态
.状态
、状态
.已创建< /code>,
, 州
.修改
, 学位
.id
, 学位
.<代码>rank_id学位
。名称
,学位
。状态
,学位
。已创建< /code>,
学校
.id
, 学校
.名称
, 学校
.<代码>地址1, 学校
.地址2
、学校
.城市
、学校
.zip_code< /code>,
学校
.country_id
, 学校
.state_id
, 学校
.<代码>电话号码, 学校
.owner_name
、学校
.建立日期
、学校
.total_competitors< /code>,
学校
.状态
, 学校
.已创建
, 学校
.<代码>修改, 交易
.id
、交易
.competitor_id
、交易
.noncompetitor_id< /code>,
交易
.创建
, 交易
.修改
, 交易
.mc_gross
、交易
.address_status
、交易
.payer_id< /code>,
交易
.address_street
, 交易
.payment_date
, 交易
.payment_status
、交易
.address_zip
、交易
.first_name< /code>,
交易
.address_country_code
, 交易
.address_name
, 交易
.<代码>自定义, 交易
.payer_status
、交易
.address_country
、交易
.address_city< /code>,
交易
.payer_email
, 交易
.verify_sign
, 交易
.txn_id
、交易
.payment_type
、交易
.last_name< /code>,
交易
.address_state
, 交易
.receiver_email
, 交易
.item_name
、交易
.mc_currency
、交易
.item_number< /code>,
交易
.residence_country
, 交易
.transaction_subject
, 交易
.payment_gross
、交易
.运费
、交易
.test_ipn< /code>,
交易
.pending_reason
FROM competitors
AS Competitor
left JOIN event_competitors AS EventCompetitor
ON (EventCompetitor
.event_id
= 3 AND EventCompetitor
.is_black
= 0 AND EventCompetitor
.is_adult
= 0) LEFT JOIN ranks
AS Rank
ON (竞争对手
.rank_id
= Rank
.id
) LEFT JOIN 锦标赛
AS 锦标赛
ON (竞争对手
.tournament_id
= 锦标赛
.id
) LEFT JOIN 国家/地区
AS 国家/地区
ON (竞争对手
.country_id
= 国家/地区
.id
) LEFT JOIN states
AS State
ON (竞争对手
.state_id
= State.<代码>id) LEFT JOIN
= 11 AND 学位
AS 学位
ON (竞争对手
.Degree_id
= 学位
.id
) LEFT JOIN schools
AS School
ON (竞争对手
.school_id
= 学校
.id
) LEFT JOIN 交易
AS 交易
ON (交易
.competitor_id
= 竞争对手
.id
) WHERE 竞争对手
.状态
= 1 AND 竞争对手
.payment_completed
= 1 AND 竞争对手
.weightgroup_id
= 13 AND 竞争对手
.< code>rank_id竞争对手
.level_id
= '0' AND Competitor
.gender
= 'Female' GROUP BY Competitor
.id
这是上面查询中的左连接条件参考:
left JOIN event_competitors AS EventCompetitor ON (EventCompetitor.event_id = 3 AND EventCompetitor.is_black = 0 AND EventCompetitor.is_adult = 0)
Here is my complex (atleast i think it is complex)
condition to find competitors from matches schedules and relating to events.
Now I have HTBTM
relations with events_competitors
table, where multiple events have multiple competitors users entries.
Here, I have used joins
condition for joining and getting related events
with competitors
which works fine, but I also want to apply additional conditions, for is_black
(check for black belt) and is_adult
(check for adult person)
'EventCompetitor.is_black' => 0,
'EventCompetitor.is_adult' => 0,
Here I want only those competitors which have both conditions (is_black / is_adult) 0, means not eligible, but it does not applying the same, which is resulting in wrong competitors results.
Below is my whole find condition:
$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'
)
);
Any idea, how can i get those things applied into JOIN
conditions, so it is applied into results.
Thanks !
Below is SQL Dump for your ref:
SELECT Competitor
.id
, 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_reason
FROM competitors
AS Competitor
left JOIN event_competitors AS EventCompetitor
ON (EventCompetitor
.event_id
= 3 AND EventCompetitor
.is_black
= 0 AND EventCompetitor
.is_adult
= 0) LEFT JOIN ranks
AS Rank
ON (Competitor
.rank_id
= Rank
.id
) LEFT JOIN tournaments
AS Tournament
ON (Competitor
.tournament_id
= Tournament
.id
) LEFT JOIN countries
AS Country
ON (Competitor
.country_id
= Country
.id
) LEFT JOIN states
AS State
ON (Competitor
.state_id
= State
.id
) LEFT JOIN degrees
AS Degree
ON (Competitor
.degree_id
= Degree
.id
) LEFT JOIN schools
AS School
ON (Competitor
.school_id
= School
.id
) LEFT JOIN transactions
AS Transaction
ON (Transaction
.competitor_id
= Competitor
.id
) WHERE Competitor
.status
= 1 AND Competitor
.payment_completed
= 1 AND Competitor
.weightgroup_id
= 13 AND Competitor
.rank_id
= 11 AND Competitor
.degree_id
= '0' AND Competitor
.gender
= 'Female' GROUP BY Competitor
.id
Here is the left join condition from above query for ref:
left JOIN event_competitors AS EventCompetitor ON (EventCompetitor.event_id = 3 AND EventCompetitor.is_black = 0 AND EventCompetitor.is_adult = 0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该为此使用可容纳行为。更多信息请访问:http://book.cakephp.org/view/1323/Containable
3) 要注入事件 id,请将包含数组传递给查找操作:
如果关系并不总是需要 is_black 和 is_adult,则您需要移动这些条件来自模型,并根据需要通过查找操作的包含参数传递它们。
You should be using the containable behavior for this. More at: http://book.cakephp.org/view/1323/Containable
3) To inject the event id, pass a contain array to your find operation:
If is_black and is_adult are not always required for the relationship, you would want to move those conditions from the model and pass them in via the contain parameter of the find operation as needed.