在cakephp 3中构建困难的计算字段

在cakephp 3中构建困难的计算字段,php,mysql,cakephp,cakephp-3.0,Php,Mysql,Cakephp,Cakephp 3.0,我有一个表,其中列出了用户。在这里,我们有字段id,rating,active。例如: id | rating | active | group (computed) 1 | 12 | 1 | 2 | 1 | 0 | 3 | 25 | 1 | 4 | 20 | 1 | 5 | 21 | 0 | 6 | 12 | 1 | ... 我的目标是使用其他计算组字段扩展这些字段。

我有一个表,其中列出了用户。在这里,我们有字段id,rating,active。例如:

id | rating | active | group (computed)
1  | 12     | 1      | 
2  | 1      | 0      |
3  | 25     | 1      |
4  | 20     | 1      |
5  | 21     | 0      |
6  | 12     | 1      |
...
我的目标是使用其他计算组字段扩展这些字段。 我预计有3组用户

  • 在活动字段中为“0”的字段在组字段中为“-”
这是最简单的一个,活动区域中“1”的用户将获得两个GOUP分配

  • 评分较高的用户中“较好”的一半将在组字段中获得1分
  • 评分较差的用户中“较差”的一半将在组字段中获得2分
因此,最终的计算表应该如下所示

id | rating | active | group (computed)
1  | 12     | 1      | 1
2  | 1      | 0      | -
3  | 25     | 1      | 2
4  | 20     | 1      | 2
5  | 21     | 0      | -
6  | 12     | 1      | 1
...
所以我有一个有效的代码,但我认为这必须更好地编码,但我不知道如何

控制器中的代码:

$this->paginate = [
        'contain' => ['ExampleTableA', 'ExampleTableB']
];
$query = $this->Users->find('all')->order(['Users.first_name'=>'ASC', 'Users.last_name'=>'ASC']);

// Extending Group      
$query->formatResults(function (\Cake\Datasource\ResultSetInterface $results) {  
    return $results->map(function ($row) {
        if($row['active'] != 1){
            $row['group'] = '-';
        }
        else{                   
            $query_active = $this->UserParticipants->find('list', array('fields' => array('Users.id')))
            ->where(['Users.active' => 1])
            ->order(['Users.rating' => 'ASC','Users.id' => 'ASC']);                  

            $array = $query_active->toList();
            $length = $query_active->count();

            $firsthalf = array_slice($array, 0, $length / 2);
            $secondhalf = array_slice($array, $length / 2);                 

            if(in_array($row['id'], $firsthalf)){
                $row['group'] = 1;
            }
            if(in_array($row['id'], $secondhalf)){
                $row['group'] = 2;
            }                   
        }
        return $row;
    });
});

$userParticipants = $this->paginate($query);
我的代码的问题是,$query\u active查找程序将根据用户数量执行。我无法在地图功能中设置任何选项。 此示例代码来自以下内容:


你有什么改进或提示吗?

我对cakephp一无所知。但我认为您应该能够将
$firsthalf
$secondhalf
的计算移出匿名函数

$this->paginate=[
'包含'=>['ExampleTableA','ExampleTableB']
];
$query=$this->Users->find('all')->order(['Users.first\u name'=>'ASC','Users.last\u name'=>'ASC']);
$query\u active=$this->UserParticipants->find('list',array('fields'=>array('Users.id'))
->其中(['Users.active'=>1])
->订单(['Users.rating'=>'ASC','Users.id'=>'ASC']);
$array=$query_active->toList();
$length=$query_active->count();
$firsthalf=array\u slice($array,0,$length/2);
$secondhalf=array\u slice($array,$length/2);
//扩展组
$query->formatResults(函数(\Cake\Datasource\resultstenterface$results)使用($firsthalf,$secondhalf){
返回$results->map(函数($row)使用($firsthalf,$secondhalf){
如果($row['active']!=1){
$row['group']='-';
}
否则{
if(在_数组中($row['id',$firsthalf)){
$row['group']=1;
}
if(在数组中($row['id',$secondhalf)){
$row['group']=2;
}                   
}
返回$row;
});
});
$userParticipants=$this->paginate($query);

这也是我第一次尝试的内容,但是当我调试variable@Yddap17哪个变量?哪一行?你用
use($firsthalf,$secondhalf)
传递变量了吗?我没有用
use
尝试过,但是当我在
中尝试
$firsthalf
$secondhalf
变量时,你的例子就成功了!忘记在第一个匿名函数中添加
use
。非常感谢!