我有一个 csv 脚本,它接受 csv 记录并导入到 mysql 数据库中。问题是 cakephp 没有成功保存数据,而 cakephp 没有给出任何验证错误。如果我从保存中删除 parent_id 或 id,那么它会成功导入所有记录,但是当我添加 id 和 parent id 列时,它会返回不保存。有时它会添加第一条记录然后停止。当我浏览代码时,cake 正在尝试查找已经存在的记录。即使存在现有 ID,我也希望它进行插入。
public function admin_uploadTopics() {
$enabled = true;
if($enabled) {
$response = array();
$response['success'] = 0;
$response['failure'] = 0;
//Represents a record set row number
$cr = 0;
//will hold each column field name
$field = array();
//variable that will store the entire record set
$d = array();
//opens csv file
if (($handle = fopen(APP."../topics", "r")) !== FALSE) {
//loops through each line
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
//creates a row
$d[$cr] = array();
for ($c=0; $c < $num; $c++) {
//if this is the first r, gather the field names
if($cr == 0) {
$field[$c] = $data[$c];
} else {//else gather the data
if($c == 0) {
$d[$cr]['Topic'] = array();
}//this constructs a array that puts the data with the correct field name
//this is the second row (1) but I want it to begin at 0 so I do cr-1
$d[$cr-1]['Topic'][$field[$c]] = $data[$c];
}
}
if($cr) {
//reset model
$this->Topic->create();
//save the row
//$d[$cr-1]['Topic']['id'] = '';
//$d[$cr-1]['Topic']['parent_id'] = '';
if($this->Topic->save($d[$cr-1], false)) {
$response['success'] += 1;
} else {
$response['failure'] += 1;
}
}
//create new record row
$cr++;
}
fclose($handle);
}
echo "There were ".$response['failure']." errors and ".$response['success']." success when resetting.";
exit();
}
exit();
}