在试图填充数据库时,cakephp由于插入而无法保存


trying to populate a database, cakephp does not save because of insert

我有一个csv脚本,它获取csv记录并导入到mysql数据库中。问题是cakepp没有成功保存数据,而且cakepp也没有给出任何验证错误。如果我从保存中删除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();
}

尝试保存

if($this->Category->save($data, false)) {
        echo "Saved!<br/>";
    }
    else{
        pr($this->validateErrors($this->Category));
    } 

你也需要看看这个数组。

$t = array(
        'id'=>(isset($cat['id'])?$topic['id']:""),
        'parent_id'=>(isset($topic['parent_id'])?$topic['parent_id']:""),
        'lft'=>(isset($cat['lft'])?$topic['lft']:""),
        'rght'=>(isset($cat['rght'])?$topic['rght']:""),
        'name'=>(isset($cat['name'])?$topic['name']:""),
        'description'=>(isset($cat['description'])?$topic['description']:""),
        'is_active'=>(isset($cat['is_active'])?$topic['is_active']:""),
        'zin_members_only'=>(isset($cat['zin_members_only'])?$topic['zin_members_only']:""),
        'not_show_submit_request_link'=>(isset($topic['not_show_submit_request_link'])?$cat['not_show_submit_request_link']:"")
    );

您已经在这些行中多次使用$topic,但从$topic的来源,我找不到