我是Cakephp的新手,我试图弄清楚它的逻辑。所以
我的数据库中有以下3个表:
customers[id, name, surname, phone, text, balance, created]
service_types[id, title, price, length, is_subscription, created]
customer_service_types[id, customer_id, service_type_id, price, created]
我想创建一个新的view
来列出所有Expiring Services
,其中将包含3列:Customer | Service Type | Exp Date
由Exp Date
排序,第一个将是快过期。
(Exp Date将根据service_type
的长度和customer_service_types
的创建日期来计算)
我必须每天在数据库中创建一个新表并填充它吗?或者,我可以创建类似侦听器的内容,这样当我打开此页面时,将在现有表中使用joins
填充网格,而无需创建新的数据库表?
编辑 v2
在等待答案的同时,我创建了一个Template/ExpiringServices/index.ctp
文件:
<h3><?= __('Expiring Services') ?></h3>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('customer') ?></th>
<th scope="col"><?= $this->Paginator->sort('service type') ?></th>
<th scope="col"><?= $this->Paginator->sort('expiring_date') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($expiringServices as $expiringService): ?>
<tr>
<td><?= h($expiringService["customer"]) ?></td>
<td><?= h($expiringService["title"]) ?></td>
<td><?= h($expiringService["expiring_date"]) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>
还有一个Controller/ExpiringServicesController.php
文件:
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Datasource\ConnectionManager;
/**
* ServiceTypes Controller
*
* @property \App\Model\Table\ServiceTypesTable $ServiceTypes
*
* @method \App\Model\Entity\ServiceType[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class ExpiringServicesController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|void
*/
public function index()
{
$conn = ConnectionManager::get('default');
$stmt = $conn->execute('SELECT c.id as customerid, CONCAT(c.name, " ", c.surname) as customer, s.title as title, s.length, cs.created , DATE_ADD(cs.created, INTERVAL s.length DAY) as expiring_date
FROM customers c
JOIN customer_service_types cs on c.id = cs.customer_id
JOIN service_types s on cs.service_type_id = s.id
WHERE s.is_subscription = 1
ORDER BY expiring_date');
//debug($stmt,true);
$expiringServices = $stmt->fetchAll('assoc');
//debug($expiringServices,true);
$this->set(compact('expiringServices'));
}
}
但是Paginator无法正常工作。我收到这些错误:
通知(8):未定义索引:pageCount [CORE \ src \ View \ Helper \ PaginatorHelper.php,第1015行]
通知(8):未定义索引:pageCount [CORE \ src \ View \ Helper \ PaginatorHelper.php,第760行]
通知(8):未定义索引:pageCount [CORE \ src \ View \ Helper \ PaginatorHelper.php,第1075行]
通知(8):未定义索引:pageCount [CORE \ src \ View \ Helper \ PaginatorHelper.php, 行686] 注意(8):未定义索引:页面[CORE \ src \ View \ Helper \ PaginatorHelper.php,第700行]
通知(8):未定义索引:当前[CORE \ src \ View \ Helper \ PaginatorHelper.php, 702行] 通知(8):未定义索引:计数 [CORE \ src \ View \ Helper \ PaginatorHelper.php,第703行]注意(8): 未定义的索引:开始[CORE \ src \ View \ Helper \ PaginatorHelper.php,行 704]
通知(8):未定义索引:结束 [CORE \ src \ View \ Helper \ PaginatorHelper.php,第705行]