gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:04:27 25 4
gpt4 key购买 nike

$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 个文件,则只保存最后一行,但保存所有行的所有事件,我尝试调试它,我回显每个循环和它读取所有行,我尝试在保存方法之后插入一个回显,以了解它是否成功并且它有效,但是当我在数据库中检查它时,只保存最后一行,但所有事件都保存了。请帮助我,谢谢。

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

最佳答案

使用 $this->Activity->clear() 重置模型;
作为 CakePHP 的书 states :

When calling save in a loop, don’t forget to call clear().

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

This method can be used to reset model state and clear out any unsaved data and validation errors.

关于php - 无法在 cakephp 中保存来自 excel 的循环数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27597559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com