我在 CakePHP 2.2.1 安装上使用 CakeDC 的 Utils 插件的 csvUpload 行为。
我让它工作得很好,它成功地处理了一个相当大的 csv。但是,我的表/模型中有两个字段被认为是固定的,因为它们基于来自不一致的关联模型的 ID。所以我需要通过变量来获得这些固定值,这很容易。
所以我的问题是,如何使用 csvUpload 的固定字段方面?我已经尝试了以下和许多小的变化,这显然没有用。
public function upload_csv($Id = null) {
$unique_add = 69;
if ( $this->request->is('POST') ) {
$records_count = $this->Model->find( 'count' );
try {
$fixed = array('Model' => array('random_id' => $Id, 'unique_add' => $unique_add));
$this->Model->importCSV($this->request->data['Model']['CsvFile']['tmp_name'], $fixed);
} catch (Exception $e) {
$import_errors = $this->Model->getImportErrors();
$this->set( 'import_errors', $import_errors );
$this->Session->setFlash( __('Error Importing') . ' ' . $this->request->data['Model']['CsvFile']['name'] . ', ' . __('column name mismatch.') );
$this->redirect( array('action'=>'import') );
}
$new_records_count = $this->Model->find( 'count' ) - $records_count;
$this->Session->setFlash(__('Successfully imported') . ' ' . $new_records_count . ' records from ' . $this->request->data['Model']['CsvFile']['name'] );
$this->redirect(array('plugin'=>'usermgmt', 'controller'=>'users', 'action'=>'dashboard'));
}
}
任何帮助将不胜感激,因为我在搜索时只找到了 1 篇关于此行为的帖子......