0

I have a csv script that takes a csv records and imports into a mysql database. The problem is cakephp is unsuccessfully saving the data and cakephp is not giving any validation errors. If I remove the parent_id or id from saving, then it successfully imports all the records, but when I add id and parent id column, it goes back to not saving. sometimes it adds the first record then stops. When I walk through the code, cake is trying to find a record that already exist. I want it to do a insert even though there is a existing 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();
}
2
  • where is $topic coming from ? did you meant foreach($d['Topic'] as $topic) ? Commented Sep 13, 2012 at 12:10
  • ok, I updated to the new code I am using. Pretty much the issue is this. Cakephp rejects the save because it can not find the ID. The records I am pull from the CSV have id's, I need to import these ID into the database. How do I get cakephp to do a INSERT with records that have a ID instead of a update Commented Sep 14, 2012 at 15:34

1 Answer 1

1

try this with save

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

    } 

you need to have a look on this array too.

$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']:"")
    );

You have used $topic several time in these lines but from where $topic is coming I can not find

Sign up to request clarification or add additional context in comments.

2 Comments

sorry, $topic is suppose to be $cat, I edited my code above. I am trying out your first method.
Thanks, this assisted in finding out why my form didn't want to save. Turns out one of my values were declared incorrectly. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.