CakePHP" saveAll"需要3分钟以上才能保存?

时间:2014-09-08 13:02:46

标签: php mysql cakephp phpmyadmin

我正在使用CakePHP制作应用程序。我做了一个使用saveAll函数的动作。

我认为它运作良好,因为它不需要那么多数据,但使用saveAll或其他保存功能需要花费3分钟以上来保存。

有没有人发现我的错误?

phpMyadmin的专栏: id,rank,school_detail_id,total_score,school_name, (大约有300~400个数据)

public function rank_update(){
        $check_scores = $this->ClubScore->find('all', array('fields'=>array('id','total_score')));
        $check_scores2 = Set::sort($check_scores, "{n}.ClubScore.total_score","DESC");

        $rank_id=0;
        $temp_score=0;
        $temp = null;
        $for_count=0;
        foreach ($check_scores2 as $check_score):
          if($temp_score != $check_score['ClubScore']['total_score']){
               $rank_id++;
               $temp_score = $check_score['ClubScore']['total_score'];
               // make ranking by score. same score is same ranking.
            }
           $this->ClubScore->id = $check_score['ClubScore']['id'];
           $this->ClubScore->saveField('rank', $rank_id);
       endforeach;
}

2 个答案:

答案 0 :(得分:0)

将查询从foreach划分为更简单的方法

以desc顺序获得不同的total_score

 $data = $this->ClubScore->find('all', array('fields'=>array('DISTINCT total_score'), 'order' => 'total_score DESC'));

然后使用updateAll和foreach

将密钥保存为每个total_score的排名

答案 1 :(得分:0)

非常感谢Abhishek和AgRizzo,Nunser !! 现在,我已经彻底解决了这个问题。它只需要1或2秒!!!!!

这是源代码。

public function rank_update(){

            $data = $this->ClubScore->find('all', array('fields'=>array('DISTINCT total_score'),       'order' => 'total_score DESC'));

            $check_scores = $this->ClubScore->find('all', array('fields'=>array('id','total_score')));

            $check_scores2 = Set::sort($check_scores, "{n}.ClubScore.total_score","DESC");

                $ii = 0;
                $temp = 0;
            foreach($check_scores2 as $scores):
                if($data[$ii]['ClubScore']['total_score']
                                == $scores['ClubScore']['total_score']){
                $temp=$ii+1;

                }else{
                $ii++;
                $temp=$ii+1;
                }

                $update_arr[] = array(
                    'ClubScore' => array(
                    'id' => $scores['ClubScore']['id'],
                    'rank' =>$temp,
                        )
                    );

            endforeach;

            $update_arr = Set::sort($update_arr, "{n}.ClubScore.id","ASC");

            var_dump($update_arr);

            foreach($update_arr as $update_arrs):
            $this->ClubScore->updateAll(
                array(
                    'ClubScore.rank' => $update_arrs['ClubScore']['rank'],
                ),
                array(
                    'ClubScore.id' => $update_arrs['ClubScore']['id'],
                    )
                );
            endforeach;


}

非常感谢你。 最好的问候。