微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CakePhP3 分页虚拟字段不起作用

如何解决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>
.....

它按我的预期工作,并正确显示注册的总访问次数和当年的访问次数

问题是当我试图将这些虚拟字段添加分页中以便按它们对结果进行排序时。

O 在控制器上,我在分页白名单中添加了这些字段:

(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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?