无法在cakephp中保存来自excel的循环数据

时间:2014-12-22 06:29:08

标签: php excel loops cakephp

$inputFileName = $_FILES["file"]["tmp_name"];       
            $objReader = PHPExcel_IOFactory::createReader('Excel5');
            $objReader->setReadDataOnly(true);
            $objPHPExcel = $objReader->load($inputFileName);
            $objWorksheet = $objPHPExcel->getActiveSheet();

            $highestRow = $objWorksheet->getHighestRow(); // e.g. 10
            $highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'

            $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
            $Player_name = '';
            $Activity_name = '';
            $Domain_id = '';
            $Activity_description = '';
            $Activity_date;
            $player_id_owner = '';
            $DomainCount = '';
            echo '<table>' . "\n";
            for ($row = 5; $row <= $highestRow -1; ++$row) 
            {
              echo '<tr>' . "\n";

              for ($col = 0; $col <= $highestColumnIndex; ++$col) 
              {
                echo '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>' . "\n";


                $Domain_name = $objWorksheet->getCellByColumnAndRow(0, $row)->getValue();                                       
                $Activity_name = $objWorksheet->getCellByColumnAndRow(1, $row)->getValue();                                     
                $Activity_description = $objWorksheet->getCellByColumnAndRow(2, $row)->getValue();
                $Player_name = $objWorksheet->getCellByColumnAndRow(8, $row)->getValue();                       
                $Activity_date = $objWorksheet->getCellByColumnAndRow(10, $row)->getFormattedValue();

            echo '</tr>' . "\n";

            echo '</table>' . "\n";

            }

                $playerCount = $this->PlayerCount($Player_name);
                echo "Current player name is: ".$Player_name;
                echo "Current player count is: ".$playerCount;


                if($playerCount == 0)
                {
                    $email = str_replace(' ', '', "$Player_name@nsn.com");
                    $player_type_id = 2;
                    $password = "password123";


                    $this->Activity->create('Player');
                    $this->request->data['name'] = $Player_name;
                    $this->request->data['player_type_id'] = $player_type_id;
                    $this->request->data['email'] = $email;
                    $this->request->data['password'] = $password;
                    if ($this->Player->save($this->request->data)) 
                    {
                        $this->flashSuccess(__('Player saved successfully!'. $Player_name));
                        echo ("Player saved successfully!". $Player_name);
                    } 
                    else 
                    {
                        $this->flashError(__('Error while trying to save Player.'));
                        debug($this->Activity->validationErrors);
                    }

                }


                $color = "#99CCFF";
                $abbr = "TD";
                $description = $Domain_name;
                $player_type_id = 1;
                $player_id_owner = $this->findPlayerId($Player_name);
                //$player_id_owner = $this->Activity->findId($Player_name);
                //echo h($id = $player_id_owner['Player']['id'] );
                //debug($player_id_owner);                      

                echo "Current Domain name is: ".$Domain_name;
                $domainCount = $this->DomainCount($Domain_name);
                echo "Current Domain count is: ".$domainCount;
                if($domainCount == 0)
                {
                    $inactive = 0;
                    $this->Activity->create('Domain');
                    $this->request->data['name'] = $Domain_name;
                    $this->request->data['color'] = $color;
                    $this->request->data['abbr'] = $abbr;
                    $this->request->data['icon'] = 'fa fa-bolt';
                    $this->request->data['description'] = $description;
                    $this->request->data['player_type_id'] = $player_type_id;
                    $this->request->data['player_id_owner'] = $player_id_owner['Player']['id'] ;
                    $this->request->data['inactive'] = $inactive;
                    if ($this->Domain->save($this->request->data)) 
                    {
                        $this->flashSuccess(__('Domain saved successfully!'. $Domain_name));
                        echo ("Domain saved successfully!". $Domain_name);
                        } 
                    else 
                    {
                        $this->flashError(__('Error while trying to save domain.'));
                        debug($this->Activity->validationErrors);
                    }
                }


                $activityCount = $this->ActivityCount($Activity_name);
                echo "Current activity name is: ".$Activity_name;
                echo "Current Activity count is: ".$activityCount;

                if($activityCount == 0)
                {
                $Domain_id = $this->findDomainId($Domain_name);
                //debug($Domain_id);
                $this->Activity->create('Activity');
                $this->request->data['name'] = $Activity_name;
                $this->request->data['domain_id'] = $Domain_id['Domain']['id'];
                $this->request->data['description'] = $Activity_description;
                $this->request->data['inactive'] = 0;
                $this->request->data['new'] = 1;
                $this->request->data['xp'] = 100;
                $Activity_date = PHPExcel_Style_NumberFormat::toFormattedString($Activity_date, 'YYYY-MM-DD h:mm:ss');
                $this->request->data['created'] = $Activity_date;
                $this->request->data['reported'] = 0;
                $this->request->data['player_id_owner'] = $player_id_owner['Player']['id'];
                $this->request->data['acceptance_votes'] = 1;
                $this->request->data['rejection_votes'] = 1;
                if ($this->Activity->save($this->request->data)) 
                {
                    $this->flashSuccess(__('Activity saved successfully!'. $Activity_name));
                    echo ("Activity saved successfully!". $Activity_name);
                } 
                else 
                {
                    $this->flashError(__('Error while trying to save activity.'));
                    debug($this->Activity->validationErrors);
                }
            }       
        }

我有这个功能,可以从excel文件中读取和保存域,活动和播放器。当excel文件只包含1行时,它运行良好,但如果excel包含多个文件,则只保存最后一行但保存所有行的所有活动,我试着调试它,我回显每个循环和它读取所有行,我尝试在保存方法后插入一个echo,以确定它是否成功,但是当我在数据库中检查它时,只保存最后一行,但活动全部保存。请帮助我谢谢。

This is the sample excel data

This is the sample activity saved in database, all rows were save this is the sample domain only the last row was save This is the sample player only the last row was save also

1 个答案:

答案 0 :(得分:0)

使用$ this-&gt; Activity-&gt; clear();

重置模型

正如CakePHP书states

  

在循环中调用save时,不要忘记调用clear()。

clear方法在保存数据之前重置模型。如果您不使用clear(),则已保存行的ID仍在模型中,因此下一次调用save会覆盖以前保存的行。

请参阅CakePHP book on the clear method

  

此方法可用于重置模型状态并清除任何未保存的数据和验证错误。