如何解决CakePhP3 分页虚拟字段不起作用
我有一个项目,它的模型中有我们想要跟踪活动的帐户。其中一项活动是我们的访问/虚拟会议。所以我有另一个模型叫做访问。
在 Accounts 表中我定义了
class AccountsTable extends Table
{
...
...
$this->hasMany('Visits',[
'foreignKey' => 'visited_account_id','sort' => ['Visits.date DESC']
]);
然后在 Accounts Class I 中创建了 2 个虚拟字段,用于检索与该帐户相关的访问次数和当年的访问次数。
class Account extends Entity
{
...
...
protected $_virtual = ['number_visits','number_visits_this_year'];
protected function _getNumberVisits($title)
{
$countAccountVisits =TableRegistry::getTableLocator()->get('visits')->find('list',['conditions'=>['visits.visited_account_id ='=>$this->_properties['id']]
]);
return count($countAccountVisits->toArray());
}
protected function _getNumberVisitsThisYear($title)
{
$year = date("Y");
$countAccountVisits =TableRegistry::getTableLocator()->get('visits')->find('list',['conditions'=>['visits.visited_account_id ='=>$this->_properties['id'],'YEAR(visits.date) ='=>$year]
]);
return count($countAccountVisits->toArray());
}
然后在帐户控制器模板的 index.CTP 中
....
<tr>
<td><?= h($account->account_type->type) ?></td>
<td><?= h($account->name) ?></td>
<td><?= h($account->main_phone) ?></td>
<td><?= h($account->adress_country) ?></td>
<td><?= h($account->adress_state) ?></td>
<td><?= $account->has('parent_account') ? $this->Html->link($account->parent_account->name,['controller' => 'Accounts','action' => 'view',$account->parent_account->id]) : '' ?></td>
<!--<td> //$this->Number->format($account->main_contact_id) ?></td>-->
<td><?= $account->numberVisits ?></td>
<td><?= $account->numberVisitsThisYear ?></td>
<td><?= count($account->open_opportunities) ?></td>
.....
它按我的预期工作,并正确显示了注册的总访问次数和当年的访问次数。
问题是当我试图将这些虚拟字段添加到分页中以便按它们对结果进行排序时。
(index action)
$this->paginate = [
'contain' => ['AccountTypes','ParentAccounts','Openopportunities'],'sortWhitelist' => [
'name','adress_country','adress_state','parent_id','number_visits','number_visits_this_year'
]
// ...
页面呈现没有错误,按名称排序的其他字段工作正常,但是当我单击虚拟字段按它们排序时,页面启动以下错误:
Error: sqlSTATE[42S22]: Column not found: 1054 UnkNown column 'number_visits' in 'order clause'
$this->paginate = [
'contain' => ['AccountTypes','number_visits_this_year'
],'fields' => [
'name','number_visits_this_year'
]
];
Error: sqlSTATE[42S22]: Column not found: 1054 UnkNown column 'Accounts.number_visits' in 'field list'
哦,顺便提一下,在 index.ctp 中,我是这样调用排序的:
//...
<th scope="col"><?= $this->Paginator->sort('name') ?></th>
<th scope="col"><?= $this->Paginator->sort('main_phone') ?></th>
<th scope="col"><?= $this->Paginator->sort('adress_country') ?></th>
<th scope="col"><?= $this->Paginator->sort('adress_state') ?></th>
<th scope="col"><?= $this->Paginator->sort('parent_id') ?></th>
<th scope="col"><?= $this->Paginator->sort('number_visits','Total Visits') ?></th>
<th scope="col"><?= $this->Paginator->sort('number_visits_this_year','Visits'.date("Y")) ?></th>
//...
'fields' => [
'name','number_visits'=>function{return get_numberOfVisits()},'number_visits_this_year'=>function{return get_numberOfVisitsThisYear()}
]
但我不知道如何以及是否正确。
我该如何解决这个问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。