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

Posted

技术标签:

【中文标题】无法在 cakephp 中保存来自 excel 的循环数据【英文标题】:unable to save looped data from excel in cakephp 【发布时间】:2015-02-20 05:59:19 【问题描述】:
$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 包含超过 1 个文件,则只保存最后一行,但保存所有行的所有活动,我尝试调试它,我回显每个循环和它读取所有行,我尝试在保存方法之后插入一个回显,以了解它是否成功并且它有效,但是当我在数据库中检查它时,只保存最后一行,但所有活动都保存了。请帮助我,谢谢。

【问题讨论】:

【参考方案1】:

用 $this->Activity->clear() 重置模型;

作为 CakePHP 的书 states:

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

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

见CakePHP book on the clear method:

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

【讨论】:

以上是关于无法在 cakephp 中保存来自 excel 的循环数据的主要内容,如果未能解决你的问题,请参考以下文章

CakePHP 3 - 记录无法保存在我的登录功能中

在 MVC 中保存相关数据(cakephp)

CakePHP 如何在一个视图中显示来自三个相关模型的数据?

CakePHP 2.0 无法保存

CakePHP- 保存相关模型数据

CakePHP 无法保存某个列