J'écris du code pour télécharger une liste d'entrées à partir d'un fichier CSV et les traiter. J'utilise une boucle foreach pour parcourir chacune d'elles. Cependant, la fonction de sauvegarde du modèle extrait les entrées précédentes et les écrase. Y a-t-il un moyen d'arrêter cela? C'est mon code de contrôleur
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
)
);
}
Il y a deux méthodes.
Transmettre l'ID dans les données
$show['id'] = 0;
Essayez d'obtenir l'état et définissez l'identifiant sur 0.
$state = $model->getState();
$state->set($model->getName().'.id', 0);
if(!$model->save($show)){
...
}