Cakephp 3另一个模型的排序不起作用

时间:2017-11-03 04:06:51

标签: sorting cakephp-3.0 paginator

我有两个型号提供& Ë X penses 问题:我有列Expenses.type_of_documentExpenses.change_to,Expenses.dateProvider.rutProvider.nickname的用户列表。每个&每列都有排序,但Provider.rutProvider.nickname排序无效。

ExpensesTable.php:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->addBehavior('Timestamp');
    $this->table('expenses');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->belongsToMany('Events', [
        'foreignKey' => 'expense_id',
        'targetForeignKey' => 'event_id',
        'joinTable' => 'events_expenses'
    ]);
    $this->belongsToMany('Providers', [
        'foreignKey' => 'expense_id',
        'targetForeignKey' => 'provider_id',
        'joinTable' => 'expenses_providers'
    ]);
    $this->hasMany('ExpensesProviders', [
        'foreignKey' => 'expense_id'
    ]);
}

ProviderTable.php

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('providers');
    $this->displayField('name');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->hasMany('Products', [
        'foreignKey' => 'provider_id'
    ]);
}

ExpensesProvidersTable.php

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('expenses_providers');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->belongsTo('Providers', [
        'foreignKey' => 'provider_id'
    ]);
    $this->belongsTo('Expenses', [
        'foreignKey' => 'expense_id'
    ]);
}

ExpensesController.php

public function index(){
    $this->paginate=[‘contain’ => [‘Providers’],‘sortWhitelist’ => [‘Expenses.type_of_document’,‘Expenses.charge_to’,‘Expenses.date_of_issue’,‘Providers.rut’,‘Providers.nickname’,]];$expenses = $this->paginate($this->Expenses->find()->contain([‘Events’, ‘Providers’]));$this->set(compact(‘expenses’, ‘opt’));$this->set(’_serialize’, [‘expenses’]);}

index.ctp

            <th class="left"><?= $this->Paginator->sort('Expenses.type_of_document', 'Tipo') ?></th>
            <th class="left"><?= $this->Paginator->sort('Expenses.charge_to', 'Cargo') ?></th>
            <th class="left"><?= $this->Paginator->sort('Expenses.date_of_issue', 'Fecha') ?></th>
            <th class="left"><?= $this->Paginator->sort('Providers.rut', 'Rut') ?></th>
            <th class="left"><?= $this->Paginator->sort('Providers.nickname', 'Proveedor') ?></th>
            <th class="left"><?= $this->Paginator->sort('Expenses.document_number', 'N° Dcto.') ?></th>
            <th class="left"><?= $this->Paginator->sort('Expenses.details', 'Detalle') ?></th>
            <th class="right"><?= $this->Paginator->sort('Expenses.amount_total', 'Total') ?></th>
            <th class="left"><?= $this->Paginator->sort('Expenses.payment_status', 'Estado') ?></th>
            <th class="left"><?= $this->Paginator->sort('Expenses.way_to_pay', 'Forma') ?></th>
            <th class="left"><?= $this->Paginator->sort('Expenses.created', 'Ingreso') ?></th>
            <th class="left"><?= $this->Paginator->sort('Expenses.paid_by', 'Pagado por') ?></th>
            <!--<th class="left"><?= $this->Paginator->sort('AccountingMonths.date', 'Mes') ?></th>
            <th scope="col"><?= $this->Paginator->sort('payment_date', 'Fecha de pago') ?></th>
            <th scope="col"><?= $this->Paginator->sort('accounting_statement', 'Estado de cuenta') ?></th>-->
            <th class="actions center"><?= __('Acciones') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($expenses as $expense): ?>
        <tr>
            <td class="left"><?= $expense->type_of_document ?></td>
            <td class="left"><?= (strcmp($expense->charge_to, 'evento') == 0 and !empty($expense->events))? $expense->events[0]->code : $expense->charge_to ?></td>
            <td class="left"><?= $this->Utils->chileanTimeZone($expense->date_of_issue, 'DDMMYYYY', 'UTC') ?></td>
            <td class="left"><?= h(!empty($expense->providers)? $expense->providers[0]->rut : '') ?></td>
            <td class="left"><?= h(!empty($expense->providers)? $expense->providers[0]->nickname : '') ?></td>
            <td class="left"><?= h($expense->document_number) ?></td>
            <td class="left"><?= h($expense->detail) ?></td>
            <td class="right"><?= "$ ".$this->Number->format($expense->amount_total, ['locale' => 'es_CL']) ?></td>
            <td class="left"><?= h($expense->payment_status) ?></td>
            <td class="left"><?= h($expense->way_to_pay) ?></td>
            <td class="left"><?= $this->Utils->chileanTimeZone($expense->created, 'DDMMYYYY', 'UTC') ?></td>
            <td class="left"><?= h($expense->paid_by) ?></td>
            <td class="actions center">
                <?= $this->Html->link(__('View'), ['action' => 'view', $expense->id]) ?>
                <?= $this->Html->link(__('Edit'), ['action' => 'edit', $expense->id]) ?>
                <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $expense->id], ['confirm' => __('Estás segur@ de eliminar # {0}?', $expense->document_number)]) ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?= $this->element('pagination') ?>

订单不起作用

<?= $this->Paginator->sort('Providers.rut', 'Rut') ?> <?= $this->Paginator->sort('Providers.nickname', 'Proveedor') ?>

让我知道你有任何解决方案。

错误SQL

SELECT Expenses.id AS `Expenses__id`, Expenses.document_number AS `Expenses__document_number`, Expenses.charge_to AS `Expenses__charge_to`, Expenses.type_of_document AS `Expenses__type_of_document`, Expenses.date_of_issue AS `Expenses__date_of_issue`, Expenses.accounting_month_date AS `Expenses__accounting_month_date`, Expenses.detail AS `Expenses__detail`, Expenses.amount_total AS `Expenses__amount_total`, Expenses.payment_status AS `Expenses__payment_status`, Expenses.way_to_pay AS `Expenses__way_to_pay`, Expenses.payment_date AS `Expenses__payment_date`, Expenses.accounting_statement AS `Expenses__accounting_statement`, Expenses.paid_by AS `Expenses__paid_by`, Expenses.created AS `Expenses__created`, Expenses.modified AS `Expenses__modified`, Expenses.rut AS `Expenses__rut`, Expenses.provider_name AS `Expenses__provider_name` FROM expenses Expenses ORDER BY Providers.nickname asc LIMIT 20 OFFSET 0

网址:http://localhost/pweventos/expenses?url=expenses&sort=Providers.nickname&direction=asc

1 个答案:

答案 0 :(得分:0)

<强>Solución

ExpensesController.php

    function index(){
    $this->paginate = [
                'contain' => ['Providers'],
                'sortWhitelist' => [
                    'Expenses.type_of_document',
                    'Expenses.charge_to',
                    'Expenses.date_of_issue',
                    'Providers.rut',
                    'Providers.nickname',
    ]
    ];
    }

<强> index.ctp

 <thead>
            <tr>
 <th scope="col"><?= $this->Paginator->sort('Expenses.type_of_document', 'Tipo') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Expenses.charge_to', 'Cargo') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Expenses.date_of_issue', 'Fecha') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Providers.rut', 'Rut') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Providers.nickname', 'Proveedor') ?></th>
<th scope="actions center"><?= __('Acciones') ?></th>
            </tr>
        </thead>