- <?php
- class ProjectImportController extends BuildTask {
- public function index() {
- $csv = \League\Csv\Reader::createFromPath(BASE_PATH . '/mysite/import.csv');
- $data = $csv->fetchAssoc();
- foreach ($data as $row) {
- // $row is 1 row of the csv file containing all the columns of the file
- // the first line of the csv is used for the keys of the $row array
- $obj = new MyDataObject();
- // example normal field:
- $obj->Title = $row['Title'];
- // example image:
- // lets assume there is a filename in the csv file, and the file is in mysite/import/
- $file = trim($row['Logo']);
- if (file_exists(BASE_PATH . "/mysite/import/$file")) {
- $parentFolder = Folder::find_or_make('logos');
- $fileObj = new Image();
- $fileObj->Filename = $filename;
- $fileObj->ParentID = $parentFolder->ID;
- $fileObj->setName($file);
- $fileObj->write();
- copy(ASE_PATH . "/mysite/import/$file", BASE_PATH . "/{$obj->Filename}");
- // lets assume MyDataObject has a has_one relation to Image called Logo
- $obj->LogoID = $fileObj->ID;
- }
- $obj->write();
- // has_many and many_many relations should be done after the first $obj->write(), because then $obj has an ID which you need for relation saving
- }