1

I am writing some code to upload a list of entries from a CSV file and process them. I am using a foreach loop to cycle through each of them, however, the save function of the model is pulling the previous entries id and overwriting it. Is there a way to stop this happening? This is my controller code

public function uploadsaveshows() {
    // An array of headers that will need to be entered into a separate array to allow entry as JSON
    $detail_headers = array(
            'legal_entity',
            'incorp_num',
            'ACN',
            'url',
            'physical_street_1',
            'physical_street_2',
            'physical_town',
            'physical_state',
            'physical_postcode',
            'start_date',
            'agm_date',
            'royal',
            'stamp_duty_exempt',
            'stamp_duty_expiry',
            'insurance_expiry',
            'supp_events'
    );

    $app   = JFactory::getApplication();
    $lang  = JFactory::getLanguage();
    $model = $this->getModel();

    date_default_timezone_set('Australia/Sydney');

    $path = JPATH_COMPONENT . '/controllers/showdata.csv';

    //Load the file and pass each line into an array.
    $rows = array_map('str_getcsv', file($path));
    //Take out the first line as it is the headers.
    $header = array_shift($rows);

    //turn each of the arrays into an entry
    foreach ($rows as $row) {           
        $show = array_combine($header, $row);
        $show['first_date_start'] =  date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $show['first_date_start'])));
        $show['first_date_finish'] =  date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $show['first_date_finish'])));
        $show['second_date_start'] =  date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $show['second_date_start'])));
        $show['second_date_finish'] =  date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $show['second_date_finish'])));
        $show['third_date_start'] =  date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $show['third_date_start'])));
        $show['third_date_finish'] =  date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $show['third_date_finish'])));
        $show['start_date'] =  date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $show['start_date'])));
        $show['agm_date'] =  date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $show['agm_date'])));
        $show['insurance_expiry'] =  date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $show['insurance_expiry'])));

        foreach ($show as $keys => $value) {
            //separate each of the entries that need to be entered into an array to be stored as JSON
            if(in_array($keys, $detail_headers)){
                $details[$keys]= $value;
                unset($show[$keys]);
            }
        }

        $show['details'] = $details;

        //Save each one
        if (!$model->save($show))
        {

            return false;

        }

    }

        // Redirect to the list screen.
        $this->setRedirect(
                JRoute::_(
                        'index.php?option=' . $this->option . '&view=' . $this->view_list
                        . $this->getRedirectToListAppend(), false
                )
        );
}

1 Answer 1

1

There are two methods.

Pass the id in the data

$show['id'] = 0;

Try to get the state and set the id to 0.

$state = $model->getState();
$state->set($model->getName().'.id', 0);
if(!$model->save($show)){
...
}
1
  • That worked perfectly! Thanks, I was hoping it would be simple :-) I went for the second option because in the save function of the model it tests if the $data['id'] is empty so I have previously tried setting it to 0 but it went through to get the id from the state anyway. Thanks again May 18, 2015 at 2:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.