0

I am making an application using CakePHP. I made an action which uses saveAll function.

And I thought it works well, because it doesn't need so much data, but it took over 3 minutes to save using saveAll, or other save function.

Does anyone find my mistakes?

phpMyadmin's columns: id, rank, school_detail_id, total_score, school_name, (there is about 300~400 data)

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;
}
Brett Gregson
  • 5,867
  • 3
  • 42
  • 60
hiroki
  • 1
  • 1
  • 2
    so... I can't find any reference to ´saveAll´ anywhere in your code... – Nunser Sep 08 '14 at 13:28
  • 1
    You should leverage the database for this operation. There are many posts such as [this](http://stackoverflow.com/questions/2727138/update-the-rank-in-a-mysql-table) with a database centric solution. – AgRizzo Sep 08 '14 at 14:26

2 Answers2

0

Divide the query from foreach to simpler approach

get distinct total_score in desc order

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

and then simply save the key as rank for each total_score using updateAll and foreach

Abhishek
  • 795
  • 9
  • 20
0

Thank you very much Abhishek and AgRizzo ,Nunser!! Now, I've completely solved this problem. It takes only 1 or 2 seconds!!!!!

Here is the source code.

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;


}

Thank you very much. Best regards.

hiroki
  • 1
  • 1