CakePHP多重JOIN findAll条件问题

发布于 2024-10-31 00:29:50 字数 9110 浏览 2 评论 0原文

这是我的复杂(至少我认为很复杂)条件,用于从比赛日程和与赛事相关的内容中查找竞争对手。

现在我与 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 学位 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 = 11 AND 竞争对手.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无名指的心愿 2024-11-07 00:29:50

您应该为此使用可容纳行为。更多信息请访问: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,则您需要移动这些条件来自模型,并根据需要通过查找操作的包含参数传递它们。

You should be using the containable behavior for this. More at: http://book.cakephp.org/view/1323/Containable

  1. Add it to your Competitor model.

var $actsAs = array('Containable');
  1. Update your model relationships in your Competitor model to include the is_black and is_adult conditions:
    
var $hasAndBelongsToMany = array(
                'Competitor' => array(
                    'className' => 'Competitor',
                    'joinTable' => 'event_competitors',
                    'alias' => 'EventCompetitor',
                    'conditions' => array(
                        'EventCompetitor.is_black' => 0,
                        'EventCompetitor.is_adult' => 0
                    )
                 )
            );

3) To inject the event id, pass a contain array to your find operation:

$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']
        )
    )
);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文