我假设您有一个用于自定义实体的资源模型(否则它将不起作用)。
资源模型应该放在[Namespace]/[Module]/Model/Resource/[Entity].php.
在该资源模型中创建此方法
public function saveImportData(array $data){
if (!empty($data)) {
$columns = array('region_name', 'cost'); //add here all your column names except the autoincrement column.
$this->_getWriteAdapter()->insertOnDuplicate($this->getMainTable(), $data, $columns);
}
return $this;
}
现在在您上传文件的操作中,解析该文件并创建一个包含要插入的行的数组。
您的数组应如下所示:
$toImport = array(
array('region_name' => 'Region 1', 'cost'=>'12.99', ...rest of the fields here),
array('region_name' => 'Region 2', 'cost'=>'17.99', ...rest of the fields here),
....
)
现在你只需要调用控制器
Mage::getResourceModel('[module]/[entity]')->saveImportData($toImport);