CakePHP 3.4:使用联合结果集的分页显示重复的记录

时间:2018-10-06 17:29:44

标签: cakephp union paginate

我正在从俱乐部会员数据库中提取两种类型的会员;我有两种自定义查找器方法来提取这些方法。这些发现者工作正常。第三个自定义查找器调用其他两个,并使用Union语句创建单个结果集。结果集的内容正确。但是,在index.ctp中使用Paginate将显示完整的结果集(正确),然后重复第二个查找器的内容。联合结果集不包含重复项。看起来分页显示对合并结果感到困惑。

第三个自定义查找器中的代码:

public function findMembersByBadAddress(Query $query, array $options=[])
{
    // Find members records with bad email or mailing addresses 
    // Specify "mailing" or "email" as the "Address" option to select either bad mailing addresses or bad
    // email addresses

    // Specific fields are retrieved so both resultset have the same number of columns. This allows the 
    // Union to work.
    $fieldsToKeep = ['Members.id','Members.first_name','Members.middle_name',
        'Members.last_name','Members.suffix', 'Members.date_joined'];

    if( $options['Address'] == 'mailing' ) {
        $flag = ['MailingAddresses.is_invalid' => true];
        $contain = ['MailingAddresses'];
    } else {
        $flag = ['EmailAddresses.is_invalid' => true];
        $contain = ['EmailAddresses'];
    }

    $query = $this->find('currentFullMembers');
    $query->contain($contain);
    $query->Select($fieldsToKeep);
    $query->where($flag);
    //Log::write('debug', "Count from MembersTable:Full = " . $query->count());

    $flagQuery2 = $this->find('lifetimeMembers');
    $flagQuery2->contain($contain);
    $flagQuery2->Select($fieldsToKeep);
    $flagQuery2->where($flag);
    //Log::write('debug', "Count from MembersTable:Lifetime = " . $flagQuery2->count());

    $query->unionAll($flagQuery2);
    $query->epilog("ORDER BY Members__last_name ASC, Members__first_name ASC");
    //Log::write('debug', "Count from MembersTable = " . $query->count());

    return $query;
}

在控制器中:

                case ('M'): // bad mailing addresses  DONE
                $query = $this->Members->find('membersByBadAddress', ['Address' => 'mailing']);
                $this->set('reportTitle', 'Bad Mailing Addresses');
                break;

...

   $this->set('members', $this->paginate($query, ['limit' => $pageLimit]) );

来自index.ctp

           <table cellpadding="0" cellspacing="0" class='table-condensed'>
            <thead>
                <tr>
                    <th scope="col"><?= $this->Paginator->sort('id') ?></th>
                    <th scope="col"><?= $this->Paginator->sort('first_name') ?></th>
                    <th scope="col"><?= $this->Paginator->sort('middle_name') ?></th>
                    <th scope="col"><?= $this->Paginator->sort('last_name') ?></th>
                    <th scope="col"><?= $this->Paginator->sort('suffix') ?></th>
                    <?php
                        if( $displayJoinDate ) {
                            echo "<th scope='col'>" . $this->Paginator->sort('Date Joined') . "</th>";
                        }
                    ?>
                    <th scope="col" class="actions"><?= __('Actions') ?></th>
                </tr>
                </thead>
                <tbody>
                <?php
        //          Log::write('debug', "Count = " . $members->count());
                     foreach($members as $member) {
                    //   Log::write("debug", $member->first_name . ' ' . $member->last_name);
                        echo "<tr>";
                        echo "<td>" . $this->Number->format($member->id) . "</td>";
                        echo "<td>" . h($member->first_name) . "</td>";
                        echo "<td>" . h($member->middle_name) . "</td>";
                        echo "<td>" . h($member->last_name) . "</td>";
                        echo "<td>" . h($member->suffix) . "</td>";
                        if( $displayJoinDate ) {
                           echo "<td>" . h($member->date_joined) . "</td>";
                        }
                        echo '<td class="actions">';
                            echo $this->Html->link(__('View'), ['action' => 'view', $member->id]);
                            echo $this->Html->link(__('Edit'), ['action' => 'edit', $member->id]);
                            echo $this->Html->link(__('Renew'), ['controller' => 'Dues2', 'action' => 'add', $member->id]);
                     //      <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $id], 
                     //             ['confirm' => __('Are you sure you want to delete # {0}?', $id)]) 
                        echo "</td>";
                    echo "</tr>";
             }
                ?>
            </tbody>
        </table>

反正还有吗?

更新

我怀疑问题是由第一个SELECT语句之后而不是UNION之后的LIMIT 20 OFFSET 0引起的。 这是DebugKit的SQL日志中显示的SQL语句:

SELECT 

Suffixes.suffix AS Suffixes__suffix 从   后缀后缀

(   选择     Members.id AS Members__id,     Members.first_name AS Members__first_name,     Members.middle_name AS Members__middle_name,     Members.last_name AS Members__last_name,     成员后缀AS Members__suffix,     Member.date_joined AS Members__date_joined   从     会员会员     内部联接Dues2 Dues2 ON(       Members.member_type ='1'       AND Dues2.payment_category = 2       AND Dues2.created> ='2017-10-10 00:00:00'       AND Dues2.created <='2018-12-31 23:59:59'       AND Members.id =(Dues2.member_id)     )     左加入成员SocialMembers ON(       SocialMembers.member_type = 2       AND Members.id =(SocialMembers.full_member_id)     )     左加入email_addresses EmailAddresses ON Members.id =(EmailAddresses.member_id)   哪里     EmailAddresses.is_invalid = 1   通过...分组     会员编号   订购     Members.last_name ASC,     Members.first_name ASC   限制     20偏移0 ) 全联盟   (     选择       Members.id AS Members__id,       Members.first_name AS Members__first_name,       Members.middle_name AS Members__middle_name,       Members.last_name AS Members__last_name,       成员后缀AS Members__suffix,       Member.date_joined AS Members__date_joined     从       会员会员       左加入成员SocialMembers ON(         SocialMembers.member_type = 2         AND Members.id =(SocialMembers.full_member_id)       )       左加入email_addresses EmailAddresses ON Members.id =(EmailAddresses.member_id)     哪里       (         Members.is_deceased = 0         AND Members.member_type = 4         AND EmailAddresses.is_invalid = 1       )     订购       Members.last_name ASC,       Members.first_name ASC   ) 订购   成员__姓氏ASC,   成员__first_name ASC

0 个答案:

没有答案